home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / parse.y < prev    next >
Text File  |  1994-08-10  |  76KB  |  2,948 lines

  1. /* Yacc grammar for bash. */
  2.  
  3. /* Copyright (C) 1989 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7.    Bash is free software; you can redistribute it and/or modify it under
  8.    the terms of the GNU General Public License as published by the Free
  9.    Software Foundation; either version 1, or (at your option) any later
  10.    version.
  11.  
  12.    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13.    WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14.    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15.    for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License along
  18.    with Bash; see the file LICENSE.  If not, write to the Free Software
  19.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. %{
  22. #include <stdio.h>
  23. #include "bashtypes.h"
  24. #include <signal.h>
  25. #include "bashansi.h"
  26. #include "shell.h"
  27. #include "flags.h"
  28. #include "input.h"
  29.  
  30. #if defined (READLINE)
  31. #  include <readline/readline.h>
  32. #endif /* READLINE */
  33.  
  34. #if defined (HISTORY)
  35. #  include "bashhist.h"
  36. #  include <readline/history.h>
  37. #endif /* HISTORY */
  38.  
  39. #if defined (JOB_CONTROL)
  40. #  include "jobs.h"
  41. #endif /* JOB_CONTROL */
  42.  
  43. #if defined (ALIAS)
  44. #  include "alias.h"
  45. #endif /* ALIAS */
  46.  
  47. #if defined (PROMPT_STRING_DECODE)
  48. #include <sys/param.h>
  49. #include <time.h>
  50. #include "maxpath.h"
  51. #endif /* PROMPT_STRING_DECODE */
  52.  
  53. #define YYDEBUG 1
  54. extern int eof_encountered;
  55. extern int no_line_editing;
  56. extern int current_command_number;
  57. extern int interactive, interactive_shell, login_shell;
  58. extern int posixly_correct;
  59. extern int last_command_exit_value;
  60. extern int interrupt_immediately;
  61. extern char *shell_name, *current_host_name;
  62. extern Function *last_shell_builtin, *this_shell_builtin;
  63. #if defined (READLINE)
  64. extern int bash_readline_initialized;
  65. #endif
  66. #if defined (BUFFERED_INPUT)
  67. extern int bash_input_fd_changed;
  68. #endif
  69.  
  70. /* **************************************************************** */
  71. /*                                    */
  72. /*            "Forward" declarations                */
  73. /*                                    */
  74. /* **************************************************************** */
  75.  
  76. /* This is kind of sickening.  In order to let these variables be seen by
  77.    all the functions that need them, I am forced to place their declarations
  78.    far away from the place where they should logically be found. */
  79.  
  80. static int reserved_word_acceptable ();
  81. static int read_token ();
  82.  
  83. static void report_syntax_error ();
  84. static void handle_eof_input_unit ();
  85. static void prompt_again ();
  86. static void reset_readline_prompt ();
  87. static void print_prompt ();
  88.  
  89. /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
  90. char *ps1_prompt, *ps2_prompt;
  91.  
  92. /* Handle on the current prompt string.  Indirectly points through
  93.    ps1_ or ps2_prompt. */
  94. char **prompt_string_pointer = (char **)NULL;
  95. char *current_prompt_string;
  96.  
  97. /* The decoded prompt string.  Used if READLINE is not defined or if
  98.    editing is turned off.  Analogous to current_readline_prompt. */
  99. static char *current_decoded_prompt;
  100.  
  101. /* The number of lines read from input while creating the current command. */
  102. int current_command_line_count = 0;
  103.  
  104. /* Variables to manage the task of reading here documents, because we need to
  105.    defer the reading until after a complete command has been collected. */
  106. static REDIRECT *redir_stack[10];
  107. int need_here_doc = 0;
  108.  
  109. /* Where shell input comes from.  History expansion is performed on each
  110.    line when the shell is interactive. */
  111. static char *shell_input_line = (char *)NULL;
  112. static int shell_input_line_index = 0;
  113. static int shell_input_line_size = 0;    /* Amount allocated for shell_input_line. */
  114. static int shell_input_line_len = 0;    /* strlen (shell_input_line) */
  115.  
  116. /* Either zero or EOF. */
  117. static int shell_input_line_terminator = 0;
  118.  
  119. static REDIRECTEE redir;
  120. %}
  121.  
  122. %union {
  123.   WORD_DESC *word;        /* the word that we read. */
  124.   int number;            /* the number that we read. */
  125.   WORD_LIST *word_list;
  126.   COMMAND *command;
  127.   REDIRECT *redirect;
  128.   ELEMENT element;
  129.   PATTERN_LIST *pattern;
  130. }
  131.  
  132. /* Reserved words.  Members of the first group are only recognized
  133.    in the case that they are preceded by a list_terminator.  Members
  134.    of the second group are recognized only under special circumstances. */
  135. %token IF THEN ELSE ELIF FI CASE ESAC FOR SELECT WHILE UNTIL DO DONE FUNCTION
  136. %token IN BANG
  137.  
  138. /* More general tokens. yylex () knows how to make these. */
  139. %token <word> WORD ASSIGNMENT_WORD
  140. %token <number> NUMBER
  141. %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND
  142. %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER
  143. %token GREATER_BAR
  144.  
  145. /* The types that the various syntactical units return. */
  146.  
  147. %type <command> inputunit command pipeline
  148. %type <command> list list0 list1 simple_list simple_list1
  149. %type <command> simple_command shell_command_1 shell_command select_command
  150. %type <command> group_command function_def if_command elif_clause subshell
  151. %type <redirect> redirection redirections
  152. %type <element> simple_command_element
  153. %type <word_list> words pattern 
  154. %type <pattern> pattern_list case_clause_sequence case_clause_1 pattern_list_1
  155.  
  156. %start inputunit
  157.  
  158. %left '&' ';' '\n' yacc_EOF
  159. %left AND_AND OR_OR
  160. %right '|'
  161. %%
  162.  
  163. inputunit:    simple_list '\n'
  164.             {
  165.               /* Case of regular command.  Discard the error
  166.                  safety net,and return the command just parsed. */
  167.               global_command = $1;
  168.               eof_encountered = 0;
  169.               discard_parser_constructs (0);
  170.               YYACCEPT;
  171.             }
  172.     |    '\n'
  173.             {
  174.               /* Case of regular command, but not a very
  175.                  interesting one.  Return a NULL command. */
  176.               global_command = (COMMAND *)NULL;
  177.               YYACCEPT;
  178.             }
  179.     |
  180.         error '\n'
  181.             {
  182.               /* Error during parsing.  Return NULL command. */
  183.               global_command = (COMMAND *)NULL;
  184.               eof_encountered = 0;
  185.               discard_parser_constructs (1);
  186.               if (interactive)
  187.                 {
  188.                   YYACCEPT;
  189.                 }
  190.               else
  191.                 {
  192.                   YYABORT;
  193.                 }
  194.             }
  195.     |    yacc_EOF
  196.             {
  197.               /* Case of EOF seen by itself.  Do ignoreeof or 
  198.                  not. */
  199.               global_command = (COMMAND *)NULL;
  200.               handle_eof_input_unit ();
  201.               YYACCEPT;
  202.             }
  203.     ;
  204.  
  205. words:    
  206.             { $$ = (WORD_LIST *)NULL; }
  207.     |    words WORD
  208.             { $$ = make_word_list ($2, $1); }
  209.     ;
  210.  
  211. redirection:    '>' WORD
  212.             {
  213.               redir.filename = $2;
  214.               $$ = make_redirection (1, r_output_direction, redir);
  215.             }
  216.     |    '<' WORD
  217.             {
  218.               redir.filename = $2;
  219.               $$ = make_redirection (0, r_input_direction, redir);
  220.             }
  221.     |    NUMBER '>' WORD
  222.             {
  223.               redir.filename = $3;
  224.               $$ = make_redirection ($1, r_output_direction, redir);
  225.             }
  226.     |    NUMBER '<' WORD
  227.             {
  228.               redir.filename = $3;
  229.               $$ = make_redirection ($1, r_input_direction, redir);
  230.             }
  231.     |    GREATER_GREATER WORD
  232.             {
  233.               redir.filename = $2;
  234.               $$ = make_redirection (1, r_appending_to, redir);
  235.             }
  236.     |    NUMBER GREATER_GREATER WORD
  237.             {
  238.               redir.filename = $3;
  239.               $$ = make_redirection ($1, r_appending_to, redir);
  240.             }
  241.     |    LESS_LESS WORD
  242.             {
  243.               redir.filename = $2;
  244.               $$ = make_redirection (0, r_reading_until, redir);
  245.               redir_stack[need_here_doc++] = $$;
  246.             }
  247.     |    NUMBER LESS_LESS WORD
  248.             {
  249.               redir.filename = $3;
  250.               $$ = make_redirection ($1, r_reading_until, redir);
  251.               redir_stack[need_here_doc++] = $$;
  252.             }
  253.     |    LESS_AND NUMBER
  254.             {
  255.               redir.dest = $2;
  256.               $$ = make_redirection (0, r_duplicating_input, redir);
  257.             }
  258.     |    NUMBER LESS_AND NUMBER
  259.             {
  260.               redir.dest = $3;
  261.               $$ = make_redirection ($1, r_duplicating_input, redir);
  262.             }
  263.     |    GREATER_AND NUMBER
  264.             {
  265.               redir.dest = $2;
  266.               $$ = make_redirection (1, r_duplicating_output, redir);
  267.             }
  268.     |    NUMBER GREATER_AND NUMBER
  269.             {
  270.               redir.dest = $3;
  271.               $$ = make_redirection ($1, r_duplicating_output, redir);
  272.             }
  273.     |    LESS_AND WORD
  274.             {
  275.               redir.filename = $2;
  276.               $$ = make_redirection (0, r_duplicating_input_word, redir);
  277.             }
  278.     |    NUMBER LESS_AND WORD
  279.             {
  280.               redir.filename = $3;
  281.               $$ = make_redirection ($1, r_duplicating_input_word, redir);
  282.             }
  283.     |    GREATER_AND WORD
  284.             {
  285.               redir.filename = $2;
  286.               $$ = make_redirection (1, r_duplicating_output_word, redir);
  287.             }
  288.     |    NUMBER GREATER_AND WORD
  289.             {
  290.               redir.filename = $3;
  291.               $$ = make_redirection ($1, r_duplicating_output_word, redir);
  292.             }
  293.     |    LESS_LESS_MINUS WORD
  294.             {
  295.               redir.filename = $2;
  296.               $$ = make_redirection
  297.                 (0, r_deblank_reading_until, redir);
  298.               redir_stack[need_here_doc++] = $$;
  299.             }
  300.     |    NUMBER LESS_LESS_MINUS WORD
  301.             {
  302.               redir.filename = $3;
  303.               $$ = make_redirection
  304.                 ($1, r_deblank_reading_until, redir);
  305.               redir_stack[need_here_doc++] = $$;
  306.             }
  307.     |    GREATER_AND '-'
  308.             {
  309.               redir.dest = 0L;
  310.               $$ = make_redirection (1, r_close_this, redir);
  311.             }
  312.     |    NUMBER GREATER_AND '-'
  313.             {
  314.               redir.dest = 0L;
  315.               $$ = make_redirection ($1, r_close_this, redir);
  316.             }
  317.     |    LESS_AND '-'
  318.             {
  319.               redir.dest = 0L;
  320.               $$ = make_redirection (0, r_close_this, redir);
  321.             }
  322.     |    NUMBER LESS_AND '-'
  323.             {
  324.               redir.dest = 0L;
  325.               $$ = make_redirection ($1, r_close_this, redir);
  326.             }
  327.     |    AND_GREATER WORD
  328.             {
  329.               redir.filename = $2;
  330.               $$ = make_redirection (1, r_err_and_out, redir);
  331.             }
  332.     |    NUMBER LESS_GREATER WORD
  333.             {
  334.               redir.filename = $3;
  335.               $$ = make_redirection ($1, r_input_output, redir);
  336.             }
  337.     |    LESS_GREATER WORD
  338.             {
  339.               REDIRECT *t1, *t2;
  340.  
  341.               redir.filename = $2;
  342.               if (posixly_correct)
  343.                 $$ = make_redirection (0, r_input_output, redir);
  344.               else
  345.                 {
  346.                   t1 = make_redirection (0, r_input_direction, redir);
  347.                   redir.filename = copy_word ($2);
  348.                   t2 = make_redirection (1, r_output_direction, redir);
  349.                   t1->next = t2;
  350.                   $$ = t1;
  351.                 }
  352.             }              
  353.     |    GREATER_BAR WORD
  354.             {
  355.               redir.filename = $2;
  356.               $$ = make_redirection (1, r_output_force, redir);
  357.             }
  358.     |    NUMBER GREATER_BAR WORD
  359.             {
  360.               redir.filename = $3;
  361.               $$ = make_redirection ($1, r_output_force, redir);
  362.             }
  363.     ;
  364.  
  365. simple_command_element: WORD
  366.             { $$.word = $1; $$.redirect = 0; }
  367.     |    ASSIGNMENT_WORD
  368.             { $$.word = $1; $$.redirect = 0; }
  369.     |    redirection
  370.             { $$.redirect = $1; $$.word = 0; }
  371.     ;
  372.  
  373. redirections:    redirection
  374.             {
  375.               $$ = $1;
  376.             }
  377.     |    redirections redirection
  378.             { 
  379.               register REDIRECT *t = $1;
  380.  
  381.               while (t->next)
  382.                 t = t->next;
  383.               t->next = $2; 
  384.               $$ = $1;
  385.             }
  386.     ;
  387.  
  388. simple_command:    simple_command_element
  389.             { $$ = make_simple_command ($1, (COMMAND *)NULL); }
  390.     |    simple_command simple_command_element
  391.             { $$ = make_simple_command ($2, $1); }
  392.     ;
  393.  
  394. command:    simple_command
  395.             { $$ = clean_simple_command ($1); }
  396.     |    shell_command
  397.             { $$ = $1; }
  398.     ;
  399.  
  400. shell_command:    shell_command_1
  401.             { $$ = $1; }
  402.     |    shell_command_1 redirections
  403.             {
  404.               if ($1->redirects)
  405.                 {
  406.                   register REDIRECT *t;
  407.                   for (t = $1->redirects; t->next; t = t->next)
  408.                 ;
  409.                   t->next = $2;
  410.                 }
  411.               else
  412.                 $1->redirects = $2;
  413.               $$ = $1;
  414.             }
  415.     ;
  416.  
  417. shell_command_1: FOR WORD newlines DO list DONE
  418.             { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5); }
  419.     |    FOR WORD newlines '{' list '}'
  420.             { $$ = make_for_command ($2, add_string_to_list ("$@", (WORD_LIST *)NULL), $5); }
  421.     |    FOR WORD ';' newlines DO list DONE
  422.             { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
  423.     |    FOR WORD ';' newlines '{' list '}'
  424.             { $$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
  425.     |    FOR WORD newlines IN words list_terminator newlines DO list DONE
  426.             { $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9); }
  427.     |    FOR WORD newlines IN words list_terminator newlines '{' list '}'
  428.             { $$ = make_for_command ($2, REVERSE_LIST ($5, WORD_LIST *), $9); }
  429.  
  430.     |    CASE WORD newlines IN newlines ESAC
  431.             { $$ = make_case_command ($2, (PATTERN_LIST *)NULL); }
  432.     |    CASE WORD newlines IN case_clause_sequence newlines ESAC
  433.             { $$ = make_case_command ($2, $5); }
  434.     |    CASE WORD newlines IN case_clause_1 ESAC
  435.             { $$ = make_case_command ($2, $5); }
  436.      |    WHILE list DO list DONE
  437.             { $$ = make_while_command ($2, $4); }
  438.     |    UNTIL list DO list DONE
  439.             { $$ = make_until_command ($2, $4); }
  440.     |    select_command
  441.             { $$ = $1; }
  442.     |    if_command
  443.             { $$ = $1; }
  444.     |    subshell
  445.             { $$ = $1; }
  446.     |    group_command
  447.             { $$ = $1; }
  448.     |    function_def
  449.             { $$ = $1; }
  450.     ;
  451.  
  452. select_command:    SELECT WORD newlines DO list DONE
  453.             {
  454. #if defined (SELECT_COMMAND)
  455.               $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5);
  456. #endif
  457.             }
  458.     |    SELECT WORD newlines '{' list '}'
  459.             {
  460. #if defined (SELECT_COMMAND)
  461.               $$ = make_select_command ($2, add_string_to_list ("$@", (WORD_LIST *)NULL), $5);
  462. #endif
  463.             }
  464.     |    SELECT WORD ';' newlines DO list DONE
  465.             {
  466. #if defined (SELECT_COMMAND)
  467.               $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6);
  468. #endif
  469.             }
  470.     |    SELECT WORD ';' newlines '{' list '}'
  471.             {
  472. #if defined (SELECT_COMMAND)
  473.               $$ = make_select_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6);
  474. #endif
  475.             }
  476.     |    SELECT WORD newlines IN words list_terminator newlines DO list DONE
  477.             {
  478. #if defined (SELECT_COMMAND)
  479.               $$ = make_select_command ($2, (WORD_LIST *)reverse_list ($5), $9);
  480. #endif
  481.             }
  482.     |    SELECT WORD newlines IN words list_terminator newlines '{' list '}'
  483.             {
  484. #if defined (SELECT_COMMAND)
  485.               $$ = make_select_command ($2, (WORD_LIST *)reverse_list ($5), $9);
  486. #endif
  487.             }
  488.     ;
  489.  
  490. function_def:    WORD '(' ')' newlines group_command
  491.             { $$ = make_function_def ($1, $5); }
  492.  
  493.     |    WORD '(' ')' newlines group_command redirections
  494.             { $5->redirects = $6; $$ = make_function_def ($1, $5); }
  495.  
  496.     |    FUNCTION WORD '(' ')' newlines group_command
  497.             { $$ = make_function_def ($2, $6); }
  498.  
  499.     |    FUNCTION WORD '(' ')' newlines group_command redirections
  500.             { $6->redirects = $7; $$ = make_function_def ($2, $6); }
  501.  
  502.     |    FUNCTION WORD newlines group_command
  503.             { $$ = make_function_def ($2, $4); }
  504.  
  505.     |    FUNCTION WORD newlines group_command redirections
  506.             { $4->redirects = $5; $$ = make_function_def ($2, $4); }
  507.     ;
  508.  
  509. subshell:    '(' list ')'
  510.             { $2->flags |= CMD_WANT_SUBSHELL; $$ = $2; }
  511.     ;
  512.     
  513. if_command:    IF list THEN list FI
  514.             { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
  515.     |    IF list THEN list ELSE list FI
  516.             { $$ = make_if_command ($2, $4, $6); }
  517.     |    IF list THEN list elif_clause FI
  518.             { $$ = make_if_command ($2, $4, $5); }
  519.     ;
  520.  
  521.  
  522. group_command:    '{' list '}'
  523.             { $$ = make_group_command ($2); }
  524.     ;
  525.  
  526. elif_clause:    ELIF list THEN list
  527.             { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
  528.     |    ELIF list THEN list ELSE list
  529.             { $$ = make_if_command ($2, $4, $6); }
  530.     |    ELIF list THEN list elif_clause
  531.             { $$ = make_if_command ($2, $4, $5); }
  532.     ;
  533.  
  534. case_clause_1:    pattern_list_1
  535.     |    case_clause_sequence pattern_list_1
  536.             { $2->next = $1; $$ = $2; }
  537.     ;
  538.  
  539. pattern_list_1:    newlines pattern ')' list
  540.             { $$ = make_pattern_list ($2, $4); }
  541.     |    newlines pattern ')' newlines
  542.             { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
  543.     |    newlines '(' pattern ')' list
  544.             { $$ = make_pattern_list ($3, $5); }
  545.     |    newlines '(' pattern ')' newlines
  546.             { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
  547.     ;
  548.  
  549. case_clause_sequence:  pattern_list
  550.     |    case_clause_sequence pattern_list
  551.             { $2->next = $1; $$ = $2; }
  552.     ;
  553.  
  554. pattern_list:    newlines pattern ')' list SEMI_SEMI
  555.             { $$ = make_pattern_list ($2, $4); }
  556.     |    newlines pattern ')' newlines SEMI_SEMI
  557.             { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
  558.     |    newlines '(' pattern ')' list SEMI_SEMI
  559.             { $$ = make_pattern_list ($3, $5); }
  560.     |    newlines '(' pattern ')' newlines SEMI_SEMI
  561.             { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
  562.     ;
  563.  
  564. pattern:    WORD
  565.             { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
  566.     |    pattern '|' WORD
  567.             { $$ = make_word_list ($3, $1); }
  568.     ;
  569.  
  570. /* A list allows leading or trailing newlines and
  571.    newlines as operators (equivalent to semicolons).
  572.    It must end with a newline or semicolon.
  573.    Lists are used within commands such as if, for, while.  */
  574.  
  575. list:        newlines list0
  576.             {
  577.               $$ = $2;
  578.               if (need_here_doc)
  579.                 gather_here_documents ();
  580.              }
  581.     ;
  582.  
  583. list0:        list1
  584.     |    list1 '\n' newlines
  585.     |    list1 '&' newlines
  586.             {
  587.               if ($1->type == cm_connection)
  588.                 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
  589.               else
  590.                 $$ = command_connect ($1, (COMMAND *)NULL, '&');
  591.             }
  592.     |    list1 ';' newlines
  593.  
  594.     ;
  595.  
  596. list1:        list1 AND_AND newlines list1
  597.             { $$ = command_connect ($1, $4, AND_AND); }
  598.     |    list1 OR_OR newlines list1
  599.             { $$ = command_connect ($1, $4, OR_OR); }
  600.     |    list1 '&' newlines list1
  601.             {
  602.               if ($1->type == cm_connection)
  603.                 $$ = connect_async_list ($1, $4, '&');
  604.               else
  605.                 $$ = command_connect ($1, $4, '&');
  606.             }
  607.     |    list1 ';' newlines list1
  608.             { $$ = command_connect ($1, $4, ';'); }
  609.     |    list1 '\n' newlines list1
  610.             { $$ = command_connect ($1, $4, ';'); }
  611.     |    pipeline
  612.             { $$ = $1; }
  613.     |    BANG pipeline
  614.             {
  615.               $2->flags |= CMD_INVERT_RETURN;
  616.               $$ = $2;
  617.             }
  618.     ;
  619.  
  620. list_terminator:'\n'
  621.     |    ';'
  622.     |    yacc_EOF
  623.     ;
  624.  
  625. newlines:
  626.     |    newlines '\n'
  627.     ;
  628.  
  629. /* A simple_list is a list that contains no significant newlines
  630.    and no leading or trailing newlines.  Newlines are allowed
  631.    only following operators, where they are not significant.
  632.  
  633.    This is what an inputunit consists of.  */
  634.  
  635. simple_list:    simple_list1
  636.             {
  637.               $$ = $1;
  638.               if (need_here_doc)
  639.                 gather_here_documents ();
  640.             }
  641.     |    simple_list1 '&'
  642.             {
  643.               if ($1->type == cm_connection)
  644.                 $$ = connect_async_list ($1, (COMMAND *)NULL, '&');
  645.               else
  646.                 $$ = command_connect ($1, (COMMAND *)NULL, '&');
  647.               if (need_here_doc)
  648.                 gather_here_documents ();
  649.             }
  650.     |    simple_list1 ';'
  651.             {
  652.               $$ = $1;
  653.               if (need_here_doc)
  654.                 gather_here_documents ();
  655.             }
  656.     ;
  657.  
  658. simple_list1:    simple_list1 AND_AND newlines simple_list1
  659.             { $$ = command_connect ($1, $4, AND_AND); }
  660.     |    simple_list1 OR_OR newlines simple_list1
  661.             { $$ = command_connect ($1, $4, OR_OR); }
  662.     |    simple_list1 '&' simple_list1
  663.             {
  664.               if ($1->type == cm_connection)
  665.                 $$ = connect_async_list ($1, $3, '&');
  666.               else
  667.                 $$ = command_connect ($1, $3, '&');
  668.             }
  669.     |    simple_list1 ';' simple_list1
  670.             { $$ = command_connect ($1, $3, ';'); }
  671.     |    pipeline
  672.             { $$ = $1; }
  673.     |    BANG pipeline
  674.             {
  675.               $2->flags |= CMD_INVERT_RETURN;
  676.               $$ = $2;
  677.             }
  678.     ;
  679.  
  680. pipeline:
  681.         pipeline '|' newlines pipeline
  682.             { $$ = command_connect ($1, $4, '|'); }
  683.     |    command
  684.             { $$ = $1; }
  685.     ;
  686. %%
  687.  
  688. /* Initial size to allocate for tokens, and the
  689.    amount to grow them by. */
  690. #define TOKEN_DEFAULT_GROW_SIZE 512
  691.  
  692. /* The token currently being read. */
  693. static int current_token = 0;
  694.  
  695. /* The last read token, or NULL.  read_token () uses this for context
  696.    checking. */
  697. static int last_read_token = 0;
  698.  
  699. /* The token read prior to last_read_token. */
  700. static int token_before_that = 0;
  701.  
  702. /* If non-zero, it is the token that we want read_token to return
  703.    regardless of what text is (or isn't) present to be read.  This
  704.    is reset by read_token. */
  705. static int token_to_read = 0;
  706.  
  707. /* Global var is non-zero when end of file has been reached. */
  708. int EOF_Reached = 0;
  709.  
  710. /* yy_getc () returns the next available character from input or EOF.
  711.    yy_ungetc (c) makes `c' the next character to read.
  712.    init_yy_io (get, unget, type, location) makes the function GET the
  713.    installed function for getting the next character, makes UNGET the
  714.    installed function for un-getting a character, sets the type of stream
  715.    (either string or file) from TYPE, and makes LOCATION point to where
  716.    the input is coming from. */
  717.  
  718. /* Unconditionally returns end-of-file. */
  719. return_EOF ()
  720. {
  721.   return (EOF);
  722. }
  723.  
  724. /* Variable containing the current get and unget functions.
  725.    See ./input.h for a clearer description. */
  726. BASH_INPUT bash_input;
  727.  
  728. /* Set all of the fields in BASH_INPUT to NULL. */
  729. void
  730. initialize_bash_input ()
  731. {
  732.   bash_input.type = 0;
  733.   bash_input.name = (char *)NULL;
  734.   bash_input.location.file = (FILE *)NULL;
  735.   bash_input.location.string = (char *)NULL;
  736.   bash_input.getter = (Function *)NULL;
  737.   bash_input.ungetter = (Function *)NULL;
  738. }
  739.  
  740. /* Set the contents of the current bash input stream from
  741.    GET, UNGET, TYPE, NAME, and LOCATION. */
  742. void
  743. init_yy_io (get, unget, type, name, location)
  744.      Function *get, *unget;
  745.      int type;
  746.      char *name;
  747.      INPUT_STREAM location;
  748. {
  749.   bash_input.type = type;
  750.   FREE (bash_input.name);
  751.  
  752.   if (name)
  753.     bash_input.name = savestring (name);
  754.   else
  755.     bash_input.name = (char *)NULL;
  756.  
  757. #if defined (CRAY)
  758.   memcpy((char *)&bash_input.location.string, (char *)&location.string, sizeof(location));
  759. #else
  760.   bash_input.location = location;
  761. #endif
  762.   bash_input.getter = get;
  763.   bash_input.ungetter = unget;
  764. }
  765.  
  766. /* Call this to get the next character of input. */
  767. yy_getc ()
  768. {
  769.   return (*(bash_input.getter)) ();
  770. }
  771.  
  772. /* Call this to unget C.  That is, to make C the next character
  773.    to be read. */
  774. yy_ungetc (c)
  775.      int c;
  776. {
  777.   return (*(bash_input.ungetter)) (c);
  778. }
  779.  
  780. #if defined (BUFFERED_INPUT)
  781. int
  782. input_file_descriptor ()
  783. {
  784.   switch (bash_input.type)
  785.     {
  786.     case st_stream:
  787.       return (fileno (bash_input.location.file));
  788.     case st_bstream:
  789.       return (bash_input.location.buffered_fd);
  790.     default:
  791.       return (fileno (stdin));
  792.     }
  793. }
  794. #endif /* BUFFERED_INPUT */
  795.  
  796. /* **************************************************************** */
  797. /*                                    */
  798. /*          Let input be read from readline ().            */
  799. /*                                    */
  800. /* **************************************************************** */
  801.  
  802. #if defined (READLINE)
  803. char *current_readline_prompt = (char *)NULL;
  804. char *current_readline_line = (char *)NULL;
  805. int current_readline_line_index = 0;
  806.  
  807. static int
  808. yy_readline_get ()
  809. {
  810.   if (!current_readline_line)
  811.     {
  812.       SigHandler *old_sigint;
  813.       int line_len;
  814.  
  815.       if (!bash_readline_initialized)
  816.     initialize_readline ();
  817.  
  818. #if defined (JOB_CONTROL)
  819.       if (job_control)
  820.     give_terminal_to (shell_pgrp);
  821. #endif /* JOB_CONTROL */
  822.  
  823.       old_sigint = (SigHandler *)set_signal_handler (SIGINT, sigint_sighandler);
  824.       interrupt_immediately++;
  825.  
  826.       if (!current_readline_prompt)
  827.     current_readline_line = readline ("");
  828.       else
  829.     current_readline_line = readline (current_readline_prompt);
  830.  
  831.       interrupt_immediately--;
  832.       set_signal_handler (SIGINT, old_sigint);
  833.  
  834.       /* Reset the prompt to whatever is in the decoded value of
  835.      prompt_string_pointer. */
  836.       reset_readline_prompt ();
  837.  
  838.       current_readline_line_index = 0;
  839.  
  840.       if (!current_readline_line)
  841.     return (EOF);
  842.  
  843.       line_len = strlen (current_readline_line);
  844.       current_readline_line = xrealloc (current_readline_line, 2 + line_len);
  845.       current_readline_line[line_len++] = '\n';
  846.       current_readline_line[line_len] = '\0';
  847.     }
  848.  
  849.   if (!current_readline_line[current_readline_line_index])
  850.     {
  851.       free (current_readline_line);
  852.       current_readline_line = (char *)NULL;
  853.       return (yy_readline_get ());
  854.     }
  855.   else
  856.     {
  857.       int c = current_readline_line[current_readline_line_index++];
  858.       return (c);
  859.     }
  860. }
  861.  
  862. static int
  863. yy_readline_unget (c)
  864. {
  865.   if (current_readline_line_index && current_readline_line)
  866.     current_readline_line[--current_readline_line_index] = c;
  867.   return (c);
  868. }
  869.  
  870. void  
  871. with_input_from_stdin ()
  872. {
  873.   INPUT_STREAM location;
  874.  
  875.   location.string = current_readline_line;
  876.   init_yy_io (yy_readline_get, yy_readline_unget,
  877.           st_string, "readline stdin", location);
  878. }
  879.  
  880. #else  /* !READLINE */
  881.  
  882. void
  883. with_input_from_stdin ()
  884. {
  885.   with_input_from_stream (stdin, "stdin");
  886. }
  887. #endif    /* !READLINE */
  888.  
  889. /* **************************************************************** */
  890. /*                                    */
  891. /*   Let input come from STRING.  STRING is zero terminated.        */
  892. /*                                    */
  893. /* **************************************************************** */
  894.  
  895. static int
  896. yy_string_get ()
  897. {
  898.   register char *string;
  899.   register int c;
  900.  
  901.   string = bash_input.location.string;
  902.   c = EOF;
  903.  
  904.   /* If the string doesn't exist, or is empty, EOF found. */
  905.   if (string && *string)
  906.     {
  907.       c = *string++;
  908.       bash_input.location.string = string;
  909.     }
  910.   return (c);
  911. }
  912.  
  913. static int
  914. yy_string_unget (c)
  915.      int c;
  916. {
  917.   *(--bash_input.location.string) = c;
  918.   return (c);
  919. }
  920.  
  921. void
  922. with_input_from_string (string, name)
  923.      char *string;
  924.      char *name;
  925. {
  926.   INPUT_STREAM location;
  927.  
  928.   location.string = string;
  929.  
  930.   init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
  931. }
  932.  
  933. /* **************************************************************** */
  934. /*                                    */
  935. /*             Let input come from STREAM.            */
  936. /*                                    */
  937. /* **************************************************************** */
  938.  
  939. static int
  940. yy_stream_get ()
  941. {
  942.   int result = EOF;
  943.  
  944.   if (bash_input.location.file)
  945. #if defined (NO_READ_RESTART_ON_SIGNAL)
  946.     result = getc_with_restart (bash_input.location.file);
  947. #else
  948.     result = getc (bash_input.location.file);
  949. #endif /* !NO_READ_RESTART_ON_SIGNAL */
  950.   return (result);
  951. }
  952.  
  953. static int
  954. yy_stream_unget (c)
  955.      int c;
  956. {
  957.   return (ungetc (c, bash_input.location.file));
  958. }
  959.  
  960. void
  961. with_input_from_stream (stream, name)
  962.      FILE *stream;
  963.      char *name;
  964. {
  965.   INPUT_STREAM location;
  966.  
  967.   location.file = stream;
  968.   init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
  969. }
  970.  
  971. typedef struct stream_saver {
  972.   struct stream_saver *next;
  973.   BASH_INPUT bash_input;
  974.   int line;
  975. #if defined (BUFFERED_INPUT)
  976.   BUFFERED_STREAM *bstream;
  977. #endif /* BUFFERED_INPUT */
  978. } STREAM_SAVER;
  979.  
  980. /* The globally known line number. */
  981. int line_number = 0;
  982.  
  983. STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
  984.  
  985. push_stream ()
  986. {
  987.   STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
  988.  
  989.   xbcopy ((char *)&bash_input, (char *)&(saver->bash_input), sizeof (BASH_INPUT));
  990.  
  991. #if defined (BUFFERED_INPUT)
  992.   saver->bstream = (BUFFERED_STREAM *)NULL;
  993.   /* If we have a buffered stream, clear out buffers[fd]. */
  994.   if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
  995.     {
  996.       saver->bstream = buffers[bash_input.location.buffered_fd];
  997.       buffers[bash_input.location.buffered_fd] = (BUFFERED_STREAM *)NULL;
  998.     }
  999. #endif /* BUFFERED_INPUT */
  1000.  
  1001.   saver->line = line_number;
  1002.   bash_input.name = (char *)NULL;
  1003.   saver->next = stream_list;
  1004.   stream_list = saver;
  1005.   EOF_Reached = line_number = 0;
  1006. }
  1007.  
  1008. pop_stream ()
  1009. {
  1010.   int temp;
  1011.  
  1012.   if (!stream_list)
  1013.     EOF_Reached = 1;
  1014.   else
  1015.     {
  1016.       STREAM_SAVER *saver = stream_list;
  1017.  
  1018.       EOF_Reached = 0;
  1019.       stream_list = stream_list->next;
  1020.  
  1021.       init_yy_io (saver->bash_input.getter,
  1022.           saver->bash_input.ungetter,
  1023.           saver->bash_input.type,
  1024.           saver->bash_input.name,
  1025.           saver->bash_input.location);
  1026.  
  1027. #if defined (BUFFERED_INPUT)
  1028.       /* If we have a buffered stream, restore buffers[fd]. */
  1029.       /* If the input file descriptor was changed while this was on the
  1030.      save stack, update the buffered fd to the new file descriptor and
  1031.      re-establish the buffer <-> bash_input fd correspondence. */
  1032.       if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
  1033.         {
  1034.           if (bash_input_fd_changed)
  1035.         {
  1036.           bash_input_fd_changed = 0;
  1037.           if (default_buffered_input >= 0)
  1038.         {
  1039.           bash_input.location.buffered_fd = default_buffered_input;
  1040.           saver->bstream->b_fd = default_buffered_input;
  1041.         }
  1042.         }
  1043.       buffers[bash_input.location.buffered_fd] = saver->bstream;
  1044.         }
  1045. #endif /* BUFFERED_INPUT */
  1046.  
  1047.       line_number = saver->line;
  1048.  
  1049.       FREE (saver->bash_input.name);
  1050.       free (saver);
  1051.     }
  1052. }
  1053.  
  1054. /*
  1055.  * This is used to inhibit alias expansion and reserved word recognition
  1056.  * inside case statement pattern lists.  A `case statement pattern list'
  1057.  * is:
  1058.  *    everything between the `in' in a `case word in' and the next ')'
  1059.  *    or `esac'
  1060.  *    everything between a `;;' and the next `)' or `esac'
  1061.  */
  1062. static int in_case_pattern_list = 0;
  1063.  
  1064. #if defined (ALIAS)
  1065. /*
  1066.  * Pseudo-global variables used in implementing token-wise alias expansion.
  1067.  */
  1068.  
  1069. static int expand_next_token = 0;
  1070.  
  1071. /*
  1072.  * Pushing and popping strings.  This works together with shell_getc to 
  1073.  * implement alias expansion on a per-token basis.
  1074.  */
  1075.  
  1076. typedef struct string_saver {
  1077.   struct string_saver *next;
  1078.   int expand_alias;  /* Value to set expand_alias to when string is popped. */
  1079.   char *saved_line;
  1080.   int saved_line_size, saved_line_index, saved_line_terminator;
  1081. } STRING_SAVER;
  1082.  
  1083. STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
  1084.  
  1085. static void save_expansion ();
  1086.  
  1087. /*
  1088.  * Push the current shell_input_line onto a stack of such lines and make S
  1089.  * the current input.  Used when expanding aliases.  EXPAND is used to set
  1090.  * the value of expand_next_token when the string is popped, so that the
  1091.  * word after the alias in the original line is handled correctly when the
  1092.  * alias expands to multiple words.  TOKEN is the token that was expanded
  1093.  * into S; it is saved and used to prevent infinite recursive expansion.
  1094.  */
  1095. static void
  1096. push_string (s, expand, token)
  1097.      char *s;
  1098.      int expand;
  1099.      char *token;
  1100. {
  1101.   STRING_SAVER *temp = (STRING_SAVER *) xmalloc (sizeof (STRING_SAVER));
  1102.  
  1103.   temp->expand_alias = expand;
  1104.   temp->saved_line = shell_input_line;
  1105.   temp->saved_line_size = shell_input_line_size;
  1106.   temp->saved_line_index = shell_input_line_index;
  1107.   temp->saved_line_terminator = shell_input_line_terminator;
  1108.   temp->next = pushed_string_list;
  1109.   pushed_string_list = temp;
  1110.  
  1111.   save_expansion (token);
  1112.  
  1113.   shell_input_line = s;
  1114.   shell_input_line_size = strlen (s);
  1115.   shell_input_line_index = 0;
  1116.   shell_input_line_terminator = '\0';
  1117.   expand_next_token = 0;
  1118. }
  1119.  
  1120. /*
  1121.  * Make the top of the pushed_string stack be the current shell input.
  1122.  * Only called when there is something on the stack.  Called from shell_getc
  1123.  * when it thinks it has consumed the string generated by an alias expansion
  1124.  * and needs to return to the original input line.
  1125.  */
  1126. static void
  1127. pop_string ()
  1128. {
  1129.   STRING_SAVER *t;
  1130.  
  1131.   FREE (shell_input_line);
  1132.   shell_input_line = pushed_string_list->saved_line;
  1133.   shell_input_line_index = pushed_string_list->saved_line_index;
  1134.   shell_input_line_size = pushed_string_list->saved_line_size;
  1135.   shell_input_line_terminator = pushed_string_list->saved_line_terminator;
  1136.   expand_next_token = pushed_string_list->expand_alias;
  1137.  
  1138.   t = pushed_string_list;
  1139.   pushed_string_list = pushed_string_list->next;
  1140.   free((char *)t);
  1141. }
  1142.  
  1143. static void
  1144. free_string_list ()
  1145. {
  1146.   register STRING_SAVER *t = pushed_string_list, *t1;
  1147.  
  1148.   while (t)
  1149.     {
  1150.       t1 = t->next;
  1151.       FREE (t->saved_line);
  1152.       free ((char *)t);
  1153.       t = t1;
  1154.     }
  1155.   pushed_string_list = (STRING_SAVER *)NULL;
  1156. }
  1157.  
  1158. /* This is a stack to save the values of all tokens for which alias
  1159.    expansion has been performed during the current call to read_token ().
  1160.    It is used to prevent alias expansion loops:
  1161.  
  1162.       alias foo=bar
  1163.       alias bar=baz
  1164.       alias baz=foo
  1165.  
  1166.    Ideally this would be taken care of by push and pop string, but because
  1167.    of when strings are popped the stack will not contain the correct
  1168.    strings to test against.  (The popping is done in shell_getc, so that when
  1169.    the current string is exhausted, shell_getc can simply pop that string off
  1170.    the stack, restore the previous string, and continue with the character
  1171.    following the token whose expansion was originally pushed on the stack.)
  1172.  
  1173.    What we really want is a record of all tokens that have been expanded for
  1174.    aliases during the `current' call to read_token().  This does that, at the
  1175.    cost of being somewhat special-purpose (OK, OK vile and unclean). */
  1176.  
  1177. typedef struct _exp_saver {
  1178.       struct _exp_saver *next;
  1179.       char *saved_token;
  1180. } EXPANSION_SAVER;
  1181.  
  1182. EXPANSION_SAVER *expanded_token_stack = (EXPANSION_SAVER *)NULL;
  1183.  
  1184. static void
  1185. save_expansion (s)
  1186.      char *s;
  1187. {
  1188.   EXPANSION_SAVER *t;
  1189.  
  1190.   t = (EXPANSION_SAVER *) xmalloc (sizeof (EXPANSION_SAVER));
  1191.   t->saved_token = savestring (s);
  1192.   t->next = expanded_token_stack;
  1193.   expanded_token_stack = t;
  1194. }
  1195.  
  1196. /* Return 1 if TOKEN has already been expanded in the current `stack' of
  1197.    expansions.  If it has been expanded already, it will appear as the value
  1198.    of saved_token for some entry in the stack of expansions created for the
  1199.    current token being expanded. */
  1200. static int
  1201. token_has_been_expanded (token)
  1202.      char *token;
  1203. {
  1204.   register EXPANSION_SAVER *t = expanded_token_stack;
  1205.  
  1206.   while (t)
  1207.     {
  1208.       if (STREQ (token, t->saved_token))
  1209.     return (1);
  1210.       t = t->next;
  1211.     }
  1212.   return (0);
  1213. }
  1214.  
  1215. static void
  1216. free_expansion_stack ()
  1217. {
  1218.   register EXPANSION_SAVER *t = expanded_token_stack, *t1;
  1219.  
  1220.   while (t)
  1221.     {
  1222.       t1 = t->next;
  1223.       free (t->saved_token);
  1224.       free (t);
  1225.       t = t1;
  1226.     }
  1227.   expanded_token_stack = (EXPANSION_SAVER *)NULL;
  1228. }
  1229.  
  1230. #endif /* ALIAS */
  1231.  
  1232. /* Return a line of text, taken from wherever yylex () reads input.
  1233.    If there is no more input, then we return NULL.  If REMOVE_QUOTED_NEWLINE
  1234.    is non-zero, we remove unquoted \<newline> pairs.  This is used by
  1235.    read_secondary_line to read here documents. */
  1236. static char *
  1237. read_a_line (remove_quoted_newline)
  1238.      int remove_quoted_newline;
  1239. {
  1240.   static char *line_buffer = (char *)NULL;
  1241.   static int buffer_size = 0;
  1242.   int indx = 0, c, peekc, pass_next;
  1243.  
  1244.   pass_next = 0;
  1245.   while (1)
  1246.     {
  1247.       c = yy_getc ();
  1248.  
  1249.       /* Allow immediate exit if interrupted during input. */
  1250.       QUIT;
  1251.  
  1252.       if (c == 0)
  1253.     continue;
  1254.  
  1255.       /* If there is no more input, then we return NULL. */
  1256.       if (c == EOF)
  1257.     {
  1258.       if (indx == 0)
  1259.         return ((char *)NULL);
  1260.       c = '\n';
  1261.     }
  1262.  
  1263.       /* `+2' in case the final character in the buffer is a newline. */
  1264.       if (indx + 2 > buffer_size)
  1265.     if (!buffer_size)
  1266.       line_buffer = xmalloc (buffer_size = 400);
  1267.     else
  1268.       line_buffer = xrealloc (line_buffer, buffer_size += 400);
  1269.  
  1270.       /* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
  1271.      here document with an unquoted delimiter.  In this case,
  1272.      the line will be expanded as if it were in double quotes.
  1273.      We allow a backslash to escape the next character, but we
  1274.      need to treat the backslash specially only if a backslash
  1275.      quoting a backslash-newline pair appears in the line. */
  1276.       if (pass_next)
  1277.         {
  1278.       line_buffer[indx++] = c;
  1279.       pass_next = 0;
  1280.         }
  1281.       else if (c == '\\' && remove_quoted_newline)
  1282.     {
  1283.       peekc = yy_getc ();
  1284.       if (peekc == '\n')
  1285.         continue;    /* Make the unquoted \<newline> pair disappear. */
  1286.       else
  1287.         {
  1288.           yy_ungetc (peekc);
  1289.           pass_next = 1;
  1290.           line_buffer[indx++] = c;        /* Preserve the backslash. */
  1291.         }
  1292.     }
  1293.       else
  1294.     line_buffer[indx++] = c;
  1295.  
  1296.       if (c == '\n')
  1297.     {
  1298.       line_buffer[indx] = '\0';
  1299.       return (line_buffer);
  1300.     }
  1301.     }
  1302. }
  1303.  
  1304. /* Return a line as in read_a_line (), but insure that the prompt is
  1305.    the secondary prompt.  This is used to read the lines of a here
  1306.    document.  REMOVE_QUOTED_NEWLINE is non-zero if we should remove
  1307.    newlines quoted with backslashes while reading the line.  It is
  1308.    non-zero unless the delimiter of the here document was quoted. */
  1309. char *
  1310. read_secondary_line (remove_quoted_newline)
  1311.      int remove_quoted_newline;
  1312. {
  1313.   prompt_string_pointer = &ps2_prompt;
  1314.   prompt_again ();
  1315.   return (read_a_line (remove_quoted_newline));
  1316. }
  1317.  
  1318.  
  1319. /* **************************************************************** */
  1320. /*                                    */
  1321. /*                YYLEX ()                */
  1322. /*                                    */
  1323. /* **************************************************************** */
  1324.  
  1325. /* Reserved words.  These are only recognized as the first word of a
  1326.    command. */
  1327. STRING_INT_ALIST word_token_alist[] = {
  1328.   { "if", IF },
  1329.   { "then", THEN },
  1330.   { "else", ELSE },
  1331.   { "elif", ELIF },
  1332.   { "fi", FI },
  1333.   { "case", CASE },
  1334.   { "esac", ESAC },
  1335.   { "for", FOR },
  1336. #if defined (SELECT_COMMAND)
  1337.   { "select", SELECT },
  1338. #endif
  1339.   { "while", WHILE },
  1340.   { "until", UNTIL },
  1341.   { "do", DO },
  1342.   { "done", DONE },
  1343.   { "in", IN },
  1344.   { "function", FUNCTION },
  1345.   { "{", '{' },
  1346.   { "}", '}' },
  1347.   { "!", BANG },
  1348.   { (char *)NULL, 0}
  1349. };
  1350.  
  1351. /* Return the next shell input character.  This always reads characters
  1352.    from shell_input_line; when that line is exhausted, it is time to
  1353.    read the next line.  This is called by read_token when the shell is
  1354.    processing normal command input. */
  1355. static int
  1356. shell_getc (remove_quoted_newline)
  1357.      int remove_quoted_newline;
  1358. {
  1359.   int c;
  1360.  
  1361.   QUIT;
  1362.  
  1363. #if defined (ALIAS)
  1364.   /* If shell_input_line[shell_input_line_index] == 0, but there is
  1365.      something on the pushed list of strings, then we don't want to go
  1366.      off and get another line.  We let the code down below handle it. */
  1367.  
  1368.   if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
  1369.                 (pushed_string_list == (STRING_SAVER *)NULL)))
  1370. #else /* !ALIAS */
  1371.   if (!shell_input_line || !shell_input_line[shell_input_line_index])
  1372. #endif /* !ALIAS */
  1373.     {
  1374.       register int i, l;
  1375.  
  1376.       restart_read_next_line:
  1377.  
  1378.       line_number++;
  1379.  
  1380.     restart_read:
  1381.  
  1382.       /* Allow immediate exit if interrupted during input. */
  1383.       QUIT;
  1384.  
  1385.       i = 0;
  1386.       shell_input_line_terminator = 0;
  1387.  
  1388. #if defined (JOB_CONTROL)
  1389.       /* This can cause a problem when reading a command as the result
  1390.      of a trap, when the trap is called from flush_child.  This call
  1391.      had better not cause jobs to disappear from the job table in
  1392.      that case, or we will have big trouble. */
  1393.       notify_and_cleanup ();
  1394. #else /* !JOB_CONTROL */
  1395.       cleanup_dead_jobs ();
  1396. #endif /* !JOB_CONTROL */
  1397.  
  1398. #if defined (READLINE)
  1399.       if (interactive && no_line_editing)
  1400. #else
  1401.       if (interactive)
  1402. #endif
  1403.     print_prompt ();
  1404.  
  1405.       if (bash_input.type == st_stream)
  1406.     clearerr (stdin);
  1407.  
  1408.       while (c = yy_getc ())
  1409.     {
  1410.       /* Allow immediate exit if interrupted during input. */
  1411.       QUIT;
  1412.  
  1413.       if (i + 2 > shell_input_line_size)
  1414.         shell_input_line =
  1415.           xrealloc (shell_input_line, shell_input_line_size += 256);
  1416.  
  1417.       if (c == EOF)
  1418.         {
  1419.           if (bash_input.type == st_stream)
  1420.         clearerr (stdin);
  1421.  
  1422.           if (!i)
  1423.         shell_input_line_terminator = EOF;
  1424.  
  1425.           shell_input_line[i] = '\0';
  1426.           break;
  1427.         }
  1428.  
  1429.       shell_input_line[i++] = c;
  1430.  
  1431.       if (c == '\n')
  1432.         {
  1433.           shell_input_line[--i] = '\0';
  1434.           current_command_line_count++;
  1435.           break;
  1436.         }
  1437.     }
  1438.       shell_input_line_index = 0;
  1439.       shell_input_line_len = i;        /* == strlen (shell_input_line) */
  1440.  
  1441. #if defined (HISTORY)
  1442.       if (interactive && shell_input_line && shell_input_line[0])
  1443.     {
  1444.       char *expansions;
  1445.  
  1446.       expansions = pre_process_line (shell_input_line, 1, 1);
  1447.  
  1448.       free (shell_input_line);
  1449.       shell_input_line = expansions;
  1450.       shell_input_line_len = shell_input_line ?
  1451.                  strlen (shell_input_line) :
  1452.                  0;
  1453.       if (!shell_input_line_len)
  1454.         current_command_line_count--;
  1455.  
  1456.       /* We have to force the xrealloc below because we don't know the
  1457.          true allocated size of shell_input_line anymore. */
  1458.       shell_input_line_size = shell_input_line_len;
  1459.     }
  1460. #endif /* HISTORY */
  1461.  
  1462.       if (shell_input_line)
  1463.     {
  1464.       /* Lines that signify the end of the shell's input should not be
  1465.          echoed. */
  1466.       if (echo_input_at_read && (shell_input_line[0] ||
  1467.                      shell_input_line_terminator != EOF))
  1468.         fprintf (stderr, "%s\n", shell_input_line);
  1469.     }
  1470.       else
  1471.     {
  1472.       shell_input_line_size = 0;
  1473.       prompt_string_pointer = ¤t_prompt_string;
  1474.       prompt_again ();
  1475.       goto restart_read;
  1476.     }
  1477.  
  1478.       /* Add the newline to the end of this string, iff the string does
  1479.      not already end in an EOF character.  */
  1480.       if (shell_input_line_terminator != EOF)
  1481.     {
  1482.       l = shell_input_line_len;    /* was a call to strlen */
  1483.  
  1484.       if (l + 3 > shell_input_line_size)
  1485.         shell_input_line = xrealloc (shell_input_line,
  1486.                     1 + (shell_input_line_size += 2));
  1487.  
  1488.       shell_input_line[l] = '\n';
  1489.       shell_input_line[l + 1] = '\0';
  1490.     }
  1491.     }
  1492.   
  1493.   c = shell_input_line[shell_input_line_index];
  1494.  
  1495.   if (c)
  1496.     shell_input_line_index++;
  1497.  
  1498.   if (c == '\\' && remove_quoted_newline &&
  1499.       shell_input_line[shell_input_line_index] == '\n')
  1500.     {
  1501.     prompt_again ();
  1502.     goto restart_read_next_line;
  1503.     }
  1504.  
  1505. #if defined (ALIAS)
  1506.   /* If C is NULL, we have reached the end of the current input string.  If
  1507.      pushed_string_list is non-empty, it's time to pop to the previous string
  1508.      because we have fully consumed the result of the last alias expansion.
  1509.      Do it transparently; just return the next character of the string popped
  1510.      to. */
  1511.   if (!c && (pushed_string_list != (STRING_SAVER *)NULL))
  1512.     {
  1513.       pop_string ();
  1514.       c = shell_input_line[shell_input_line_index];
  1515.       if (c)
  1516.     shell_input_line_index++;
  1517.     }
  1518. #endif /* ALIAS */
  1519.  
  1520.   if (!c && shell_input_line_terminator == EOF)
  1521.     {
  1522.       if (shell_input_line_index != 0)
  1523.     return ('\n');
  1524.       else
  1525.     return (EOF);
  1526.     }
  1527.  
  1528.   return (c);
  1529. }
  1530.  
  1531. /* Put C back into the input for the shell. */
  1532. static void
  1533. shell_ungetc (c)
  1534.      int c;
  1535. {
  1536.   if (shell_input_line && shell_input_line_index)
  1537.     shell_input_line[--shell_input_line_index] = c;
  1538. }
  1539.  
  1540. /* Discard input until CHARACTER is seen. */
  1541. static void
  1542. discard_until (character)
  1543.      int character;
  1544. {
  1545.   int c;
  1546.  
  1547.   while ((c = shell_getc (0)) != EOF && c != character)
  1548.     ;
  1549.  
  1550.   if (c != EOF)
  1551.     shell_ungetc (c);
  1552. }
  1553.  
  1554. /* Place to remember the token.  We try to keep the buffer
  1555.    at a reasonable size, but it can grow. */
  1556. static char *token = (char *)NULL;
  1557.  
  1558. /* Current size of the token buffer. */
  1559. static int token_buffer_size = 0;
  1560.  
  1561. void
  1562. execute_prompt_command (command)
  1563.      char *command;
  1564. {
  1565.   Function *temp_last, *temp_this;
  1566.   char *last_lastarg;
  1567.   int temp_exit_value, temp_eof_encountered;
  1568.  
  1569.   temp_last = last_shell_builtin;
  1570.   temp_this = this_shell_builtin;
  1571.   temp_exit_value = last_command_exit_value;
  1572.   temp_eof_encountered = eof_encountered;
  1573.   last_lastarg = get_string_value ("_");
  1574.   if (last_lastarg)
  1575.     last_lastarg = savestring (last_lastarg);
  1576.  
  1577.   parse_and_execute (savestring (command), "PROMPT_COMMAND", 0);
  1578.  
  1579.   last_shell_builtin = temp_last;
  1580.   this_shell_builtin = temp_this;
  1581.   last_command_exit_value = temp_exit_value;
  1582.   eof_encountered = temp_eof_encountered;
  1583.  
  1584.   bind_variable ("_", last_lastarg);
  1585.   FREE (last_lastarg);
  1586. }
  1587.  
  1588. /* Command to read_token () explaining what we want it to do. */
  1589. #define READ 0
  1590. #define RESET 1
  1591. #define prompt_is_ps1 \
  1592.       (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
  1593.  
  1594. /* Function for yyparse to call.  yylex keeps track of
  1595.    the last two tokens read, and calls read_token.  */
  1596.  
  1597. yylex ()
  1598. {
  1599.   if (interactive && (!current_token || current_token == '\n'))
  1600.     {
  1601.       /* Before we print a prompt, we might have to check mailboxes.
  1602.      We do this only if it is time to do so. Notice that only here
  1603.      is the mail alarm reset; nothing takes place in check_mail ()
  1604.      except the checking of mail.  Please don't change this. */
  1605.       if (prompt_is_ps1 && time_to_check_mail ())
  1606.     {
  1607.       check_mail ();
  1608.       reset_mail_timer ();
  1609.     }
  1610.  
  1611.       /* Avoid printing a prompt if we're not going to read anything, e.g.
  1612.      after resetting the parser with read_token (RESET). */
  1613.       if (token_to_read == 0 && interactive)
  1614.     prompt_again ();
  1615.     }
  1616.  
  1617.   token_before_that = last_read_token;
  1618.   last_read_token = current_token;
  1619.   current_token = read_token (READ);
  1620.   return (current_token);
  1621. }
  1622.  
  1623. /* Called from shell.c when Control-C is typed at top level.  Or
  1624.    by the error rule at top level. */
  1625. reset_parser ()
  1626. {
  1627.   read_token (RESET);
  1628. }
  1629.   
  1630. /* When non-zero, we have read the required tokens
  1631.    which allow ESAC to be the next one read. */
  1632. static int allow_esac_as_next = 0;
  1633.  
  1634. /* When non-zero, accept single '{' as a token itself. */
  1635. static int allow_open_brace = 0;
  1636.  
  1637. /* DELIMITERS is a stack of the nested delimiters that we have
  1638.    encountered so far. */
  1639. static char *delimiters = (char *)NULL;
  1640.  
  1641. /* Offset into the stack of delimiters. */
  1642. int delimiter_depth = 0;
  1643.  
  1644. /* How many slots are allocated to DELIMITERS. */
  1645. static int delimiter_space = 0;
  1646.  
  1647. void
  1648. gather_here_documents ()
  1649. {
  1650.   int r = 0;
  1651.   while (need_here_doc)
  1652.     {
  1653.       make_here_document (redir_stack[r++]);
  1654.       need_here_doc--;
  1655.     }
  1656. }
  1657.  
  1658. /* Macro for accessing the top delimiter on the stack.  Returns the
  1659.    delimiter or zero if none. */
  1660. #define current_delimiter() \
  1661.   (delimiter_depth ? delimiters[delimiter_depth - 1] : 0)
  1662.  
  1663. #define push_delimiter(character) \
  1664.   do \
  1665.     { \
  1666.       if (delimiter_depth + 2 > delimiter_space) \
  1667.     delimiters = xrealloc \
  1668.       (delimiters, (delimiter_space += 10) * sizeof (char)); \
  1669.       delimiters[delimiter_depth] = character; \
  1670.       delimiter_depth++; \
  1671.     } \
  1672.   while (0)
  1673.  
  1674. /* When non-zero, an open-brace used to create a group is awaiting a close
  1675.    brace partner. */
  1676. static int open_brace_awaiting_satisfaction = 0;
  1677.  
  1678. #define command_token_position(token) \
  1679.   (((token) == ASSIGNMENT_WORD) || \
  1680.    ((token) != SEMI_SEMI && reserved_word_acceptable(token)))
  1681.  
  1682. #define assignment_acceptable(token) command_token_position(token) && \
  1683.                     (in_case_pattern_list == 0)
  1684.  
  1685. /* Check to see if TOKEN is a reserved word and return the token
  1686.    value if it is. */
  1687. #define CHECK_FOR_RESERVED_WORD(tok) \
  1688.   do { \
  1689.     if (!dollar_present && !quoted && \
  1690.     reserved_word_acceptable (last_read_token)) \
  1691.       { \
  1692.     int i; \
  1693.     for (i = 0; word_token_alist[i].word != (char *)NULL; i++) \
  1694.       if (STREQ (tok, word_token_alist[i].word)) \
  1695.         { \
  1696.           if (in_case_pattern_list && (word_token_alist[i].token != ESAC)) \
  1697.         break; \
  1698. \
  1699.           if (word_token_alist[i].token == ESAC) \
  1700.         in_case_pattern_list = 0; \
  1701. \
  1702.           if (word_token_alist[i].token == '{') \
  1703.         open_brace_awaiting_satisfaction++; \
  1704. \
  1705.           return (word_token_alist[i].token); \
  1706.         } \
  1707.       } \
  1708.   } while (0)
  1709.  
  1710. /* Read the next token.  Command can be READ (normal operation) or 
  1711.    RESET (to normalize state). */
  1712. static int
  1713. read_token (command)
  1714.      int command;
  1715. {
  1716.   int character;        /* Current character. */
  1717.   int peek_char;        /* Temporary look-ahead character. */
  1718.   int result;            /* The thing to return. */
  1719.   WORD_DESC *the_word;        /* The value for YYLVAL when a WORD is read. */
  1720.  
  1721.   if (token_buffer_size < TOKEN_DEFAULT_GROW_SIZE)
  1722.     {
  1723.       FREE (token);
  1724.       token = xmalloc (token_buffer_size = TOKEN_DEFAULT_GROW_SIZE);
  1725.     }
  1726.  
  1727.   if (command == RESET)
  1728.     {
  1729.       delimiter_depth = 0;    /* No delimiters found so far. */
  1730.       open_brace_awaiting_satisfaction = 0;
  1731.       in_case_pattern_list = 0;
  1732.  
  1733. #if defined (ALIAS)
  1734.       if (pushed_string_list)
  1735.     {
  1736.       free_string_list ();
  1737.       pushed_string_list = (STRING_SAVER *)NULL;
  1738.     }
  1739.  
  1740.       if (expanded_token_stack)
  1741.     {
  1742.       free_expansion_stack ();
  1743.       expanded_token_stack = (EXPANSION_SAVER *)NULL;
  1744.     }
  1745.  
  1746.       expand_next_token = 0;
  1747. #endif /* ALIAS */
  1748.  
  1749.       if (shell_input_line)
  1750.     {
  1751.       free (shell_input_line);
  1752.       shell_input_line = (char *)NULL;
  1753.       shell_input_line_size = shell_input_line_index = 0;
  1754.     }
  1755.       last_read_token = '\n';
  1756.       token_to_read = '\n';
  1757.       return ('\n');
  1758.     }
  1759.  
  1760.   if (token_to_read)
  1761.     {
  1762.       int rt = token_to_read;
  1763.       token_to_read = 0;
  1764.       return (rt);
  1765.     }
  1766.  
  1767. #if defined (ALIAS)
  1768.   /* If we hit read_token () and there are no saved strings on the
  1769.      pushed_string_list, then we are no longer currently expanding a
  1770.      token.  This can't be done in pop_stream, because pop_stream
  1771.      may pop the stream before the current token has finished being
  1772.      completely expanded (consider what happens when we alias foo to foo,
  1773.      and then try to expand it). */
  1774.   if (!pushed_string_list && expanded_token_stack)
  1775.     {
  1776.       free_expansion_stack ();
  1777.       expanded_token_stack = (EXPANSION_SAVER *)NULL;
  1778.     }
  1779.  
  1780.   /* This is a place to jump back to once we have successfully expanded a
  1781.      token with an alias and pushed the string with push_string () */
  1782.  re_read_token:
  1783.  
  1784. #endif /* ALIAS */
  1785.  
  1786.   /* Read a single word from input.  Start by skipping blanks. */
  1787.   while ((character = shell_getc (1)) != EOF && whitespace (character));
  1788.  
  1789.   if (character == EOF)
  1790.     {
  1791.       EOF_Reached = 1;
  1792.       return (yacc_EOF);
  1793.     }
  1794.  
  1795.   if (character == '#' && (!interactive || interactive_comments))
  1796.     {
  1797.       /* A comment.  Discard until EOL or EOF, and then return a newline. */
  1798.       discard_until ('\n');
  1799.       shell_getc (0);
  1800.  
  1801.       /* If we're about to return an unquoted newline, we can go and collect
  1802.      the text of any pending here documents. */
  1803.       if (need_here_doc)
  1804.         gather_here_documents ();
  1805.  
  1806. #if defined (ALIAS)
  1807.       expand_next_token = 0;
  1808. #endif /* ALIAS */
  1809.  
  1810.       return ('\n');
  1811.     }
  1812.  
  1813.   if (character == '\n')
  1814.     {
  1815.       /* If we're about to return an unquoted newline, we can go and collect
  1816.      the text of any pending here document. */
  1817.       if (need_here_doc)
  1818.     gather_here_documents ();
  1819.  
  1820. #if defined (ALIAS)
  1821.       expand_next_token = 0;
  1822. #endif /* ALIAS */
  1823.  
  1824.       return (character);
  1825.     }
  1826.  
  1827.   if (member (character, "()<>;&|"))
  1828.     {
  1829. #if defined (ALIAS)
  1830.       /* Turn off alias tokenization iff this character sequence would
  1831.      not leave us ready to read a command. */
  1832.       if (character == '<' || character == '>')
  1833.     expand_next_token = 0;
  1834. #endif /* ALIAS */
  1835.  
  1836.       /* Please note that the shell does not allow whitespace to
  1837.      appear in between tokens which are character pairs, such as
  1838.      "<<" or ">>".  I believe this is the correct behaviour. */
  1839.       if (character == (peek_char = shell_getc (1)))
  1840.     {
  1841.       switch (character)
  1842.         {
  1843.           /* If '<' then we could be at "<<" or at "<<-".  We have to
  1844.          look ahead one more character. */
  1845.         case '<':
  1846.           peek_char = shell_getc (1);
  1847.           if (peek_char == '-')
  1848.         return (LESS_LESS_MINUS);
  1849.           else
  1850.         {
  1851.           shell_ungetc (peek_char);
  1852.           return (LESS_LESS);
  1853.         }
  1854.  
  1855.         case '>':
  1856.           return (GREATER_GREATER);
  1857.  
  1858.         case ';':
  1859.           in_case_pattern_list = 1;
  1860. #if defined (ALIAS)
  1861.           expand_next_token = 0;
  1862. #endif /* ALIAS */
  1863.           return (SEMI_SEMI);
  1864.  
  1865.         case '&':
  1866.           return (AND_AND);
  1867.  
  1868.         case '|':
  1869.           return (OR_OR);
  1870.         }
  1871.     }
  1872.       else
  1873.     {
  1874.       if (peek_char == '&')
  1875.         {
  1876.           switch (character)
  1877.         {
  1878.         case '<': return (LESS_AND);
  1879.         case '>': return (GREATER_AND);
  1880.         }
  1881.         }
  1882.       if (character == '<' && peek_char == '>')
  1883.         return (LESS_GREATER);
  1884.       if (character == '>' && peek_char == '|')
  1885.         return (GREATER_BAR);
  1886.       if (peek_char == '>' && character == '&')
  1887.         return (AND_GREATER);
  1888.     }
  1889.       shell_ungetc (peek_char);
  1890.  
  1891.       /* If we look like we are reading the start of a function
  1892.      definition, then let the reader know about it so that
  1893.      we will do the right thing with `{'. */
  1894.       if (character == ')' &&
  1895.       last_read_token == '(' && token_before_that == WORD)
  1896.     {
  1897.       allow_open_brace = 1;
  1898. #if defined (ALIAS)
  1899.       expand_next_token = 0;
  1900. #endif /* ALIAS */
  1901.     }
  1902.  
  1903.       if (in_case_pattern_list && (character == ')'))
  1904.     in_case_pattern_list = 0;
  1905.  
  1906. #if defined (PROCESS_SUBSTITUTION)
  1907.       /* Check for the constructs which introduce process substitution.
  1908.      Shells running in `posix mode' don't do process substitution. */
  1909.       if (posixly_correct ||
  1910.       (((character == '>' || character == '<') && peek_char == '(') == 0))
  1911. #endif /* PROCESS_SUBSTITUTION */
  1912.     return (character);
  1913.     }
  1914.  
  1915.   /* Hack <&- (close stdin) case. */
  1916.   if (character == '-')
  1917.     {
  1918.       switch (last_read_token)
  1919.     {
  1920.     case LESS_AND:
  1921.     case GREATER_AND:
  1922.       return (character);
  1923.     }
  1924.     }
  1925.   
  1926.   /* Okay, if we got this far, we have to read a word.  Read one,
  1927.      and then check it against the known ones. */
  1928.   {
  1929.     /* Index into the token that we are building. */
  1930.     int token_index = 0;
  1931.  
  1932.     /* ALL_DIGITS becomes zero when we see a non-digit. */
  1933.     int all_digits = digit (character);
  1934.  
  1935.     /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
  1936.     int dollar_present = 0;
  1937.  
  1938.     /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
  1939.     int quoted = 0;
  1940.  
  1941.     /* Non-zero means to ignore the value of the next character, and just
  1942.        to add it no matter what. */
  1943.     int pass_next_character = 0;
  1944.  
  1945.     /* Non-zero means parsing a dollar-paren construct.  It is the count of
  1946.        un-quoted closes we need to see. */
  1947.     int dollar_paren_level = 0;
  1948.  
  1949.     /* Non-zero means parsing a dollar-bracket construct ($[...]).  It is
  1950.        the count of un-quoted `]' characters we need to see. */
  1951.     int dollar_bracket_level = 0;
  1952.  
  1953.     /* Non-zero means parsing a `${' construct.  It is the count of
  1954.        un-quoted `}' we need to see. */
  1955.     int dollar_brace_level = 0;
  1956.  
  1957.     /* A level variable for parsing '${ ... }' constructs inside of double
  1958.        quotes. */
  1959.     int delimited_brace_level = 0;
  1960.  
  1961.     /* A boolean variable denoting whether or not we are currently parsing
  1962.        a double-quoted string embedded in a $( ) or ${ } construct. */
  1963.     int embedded_quoted_string = 0;
  1964.  
  1965.     /* Another level variable.  This one is for dollar_parens inside of
  1966.        double-quotes. */
  1967.     int delimited_paren_level = 0;
  1968.  
  1969.     /* The current delimiting character. */
  1970.     int cd;
  1971.  
  1972.     for (;;)
  1973.       {
  1974.     if (character == EOF)
  1975.       goto got_token;
  1976.  
  1977.     if (pass_next_character)
  1978.       {
  1979.         pass_next_character = 0;
  1980.         goto got_character;
  1981.       }
  1982.  
  1983.     cd = current_delimiter ();
  1984.  
  1985.     if (cd && character == '\\' && cd != '\'')
  1986.       {
  1987.         peek_char = shell_getc (0);
  1988.         if (peek_char != '\\')
  1989.           shell_ungetc (peek_char);
  1990.         else
  1991.           {
  1992.         token[token_index++] = character;
  1993.         goto got_character;
  1994.           }
  1995.       }
  1996.  
  1997.     /* Handle backslashes.  Quote lots of things when not inside of
  1998.        double-quotes, quote some things inside of double-quotes. */
  1999.        
  2000.     if (character == '\\' && (!delimiter_depth || cd != '\''))
  2001.       {
  2002.         peek_char = shell_getc (0);
  2003.  
  2004.         /* Backslash-newline is ignored in all cases excepting
  2005.            when quoted with single quotes. */
  2006.         if (peek_char == '\n')
  2007.           {
  2008.         character = '\n';
  2009.         goto next_character;
  2010.           }
  2011.         else
  2012.           {
  2013.         shell_ungetc (peek_char);
  2014.  
  2015.         /* If the next character is to be quoted, do it now. */
  2016.         if (!cd || cd == '`' ||
  2017.             (cd == '"' && member (peek_char, slashify_in_quotes)))
  2018.           {
  2019.             pass_next_character++;
  2020.             quoted = 1;
  2021.             goto got_character;
  2022.           }
  2023.           }
  2024.       }
  2025.  
  2026.     /* This is a hack, in its present form.  If a backquote substitution
  2027.        appears within double quotes, everything within the backquotes
  2028.        should be read as part of a single word.  Jesus.  Now I see why
  2029.        Korn introduced the $() form. */
  2030.     if (delimiter_depth && (cd == '"') && (character == '`'))
  2031.       {
  2032.         push_delimiter (character);
  2033.         goto got_character;
  2034.       }
  2035.  
  2036.     cd = current_delimiter ();        /* XXX - may not need */
  2037.     if (delimiter_depth)
  2038.       {
  2039.         if (character == cd)
  2040.           {
  2041.               /* If we see a double quote while parsing a double-quoted
  2042.           $( ) or ${ }, and we have not seen ) or }, respectively,
  2043.                  note that we are in the middle of reading an embedded
  2044.            quoted string. */
  2045.         if ((delimited_paren_level || delimited_brace_level) &&
  2046.             (character == '"'))
  2047.           {
  2048.             embedded_quoted_string = !embedded_quoted_string;
  2049.             goto got_character;
  2050.           }
  2051.         
  2052.         delimiter_depth--;
  2053.         goto got_character;
  2054.           }
  2055.       }
  2056.  
  2057.     if (cd != '\'')
  2058.       {
  2059. #if defined (PROCESS_SUBSTITUTION)
  2060.         if (character == '$' || character == '<' || character == '>')
  2061. #else
  2062.         if (character == '$')
  2063. #endif /* !PROCESS_SUBSTITUTION */
  2064.           {
  2065.               /* If we're in the middle of parsing a $( ) or ${ }
  2066.                  construct with an embedded quoted string, don't
  2067.                  bother looking at this character any further. */
  2068.               if (embedded_quoted_string)
  2069.                 goto got_character;
  2070.  
  2071.         peek_char = shell_getc (1);
  2072.         shell_ungetc (peek_char);
  2073.         if (peek_char == '(')
  2074.           {
  2075.             if (!delimiter_depth)
  2076.               dollar_paren_level++;
  2077.             else
  2078.               delimited_paren_level++;
  2079.  
  2080.             pass_next_character++;
  2081.             goto got_character;
  2082.           }
  2083.         else if (peek_char == '[' && character == '$')
  2084.           {
  2085.             if (!delimiter_depth)
  2086.               dollar_bracket_level++;
  2087.  
  2088.             pass_next_character++;
  2089.             goto got_character;
  2090.           }
  2091.         /* This handles ${...} constructs. */
  2092.         else if (peek_char == '{' && character == '$')
  2093.           {
  2094.             if (!delimiter_depth)
  2095.               dollar_brace_level++;
  2096.             else
  2097.               delimited_brace_level++;
  2098.  
  2099.             pass_next_character++;
  2100.             goto got_character;
  2101.           }
  2102.           }
  2103.  
  2104.         /* If we are parsing a $() or $[] construct, we need to balance
  2105.            parens and brackets inside the construct.  This whole function
  2106.            could use a rewrite. */
  2107.         if (character == '(' && !embedded_quoted_string)
  2108.           {
  2109.         if (delimiter_depth && delimited_paren_level)
  2110.           delimited_paren_level++;
  2111.  
  2112.         if (!delimiter_depth && dollar_paren_level)
  2113.           dollar_paren_level++;
  2114.           }
  2115.  
  2116.         if (character == '[')
  2117.           {
  2118.         if (!delimiter_depth && dollar_bracket_level)
  2119.           dollar_bracket_level++;
  2120.           }
  2121.  
  2122.         if (character == '{' && !embedded_quoted_string)
  2123.           {
  2124.               if (delimiter_depth && delimited_brace_level)
  2125.                 delimited_brace_level++;
  2126.  
  2127.               if (!delimiter_depth && dollar_brace_level)
  2128.                 dollar_brace_level++;
  2129.           }
  2130.  
  2131.         /* This code needs to take into account whether we are inside a
  2132.            case statement pattern list, and whether this paren is supposed
  2133.            to terminate it (hey, it could happen).  It's not as simple
  2134.            as just using in_case_pattern_list, because we're not parsing
  2135.            anything while we're reading a $( ) construct.  Maybe we
  2136.            should move that whole mess into the yacc parser. */
  2137.         if (character == ')' && !embedded_quoted_string)
  2138.           {
  2139.         if (delimiter_depth && delimited_paren_level)
  2140.           delimited_paren_level--;
  2141.  
  2142.         if (!delimiter_depth && dollar_paren_level)
  2143.           {
  2144.             dollar_paren_level--;
  2145.             goto got_character;
  2146.           }
  2147.           }
  2148.  
  2149.         if (character == ']')
  2150.           {
  2151.         if (!delimiter_depth && dollar_bracket_level)
  2152.           {
  2153.             dollar_bracket_level--;
  2154.             goto got_character;
  2155.           }
  2156.           }
  2157.  
  2158.         if (character == '}' && !embedded_quoted_string)
  2159.           {
  2160.         if (delimiter_depth && delimited_brace_level)
  2161.           delimited_brace_level--;
  2162.  
  2163.         if (!delimiter_depth && dollar_brace_level)
  2164.           {
  2165.             dollar_brace_level--;
  2166.             goto got_character;
  2167.           }
  2168.           }
  2169.       }
  2170.  
  2171.     if (!dollar_paren_level && !dollar_bracket_level &&
  2172.         !dollar_brace_level && !delimiter_depth &&
  2173.         member (character, " \t\n;&()|<>"))
  2174.       {
  2175.         shell_ungetc (character);
  2176.         goto got_token;
  2177.       }
  2178.     
  2179.     if (!delimiter_depth)
  2180.       {
  2181.         if (character == '"' || character == '`' || character == '\'')
  2182.           {
  2183.         push_delimiter (character);
  2184.  
  2185.         quoted = 1;
  2186.         goto got_character;
  2187.           }
  2188.       }
  2189.  
  2190.     if (all_digits)
  2191.       all_digits = digit (character);
  2192.     if (character == '$')
  2193.       dollar_present = 1;
  2194.  
  2195.       got_character:
  2196.  
  2197.     if (character == CTLESC || character == CTLNUL)
  2198.       token[token_index++] = CTLESC;
  2199.  
  2200.     token[token_index++] = character;
  2201.  
  2202.     if (token_index == (token_buffer_size - 1))
  2203.       {
  2204.         token_buffer_size += TOKEN_DEFAULT_GROW_SIZE;
  2205.         token = xrealloc (token, token_buffer_size);
  2206.       }
  2207.     next_character:
  2208.     if (character == '\n' && interactive && bash_input.type != st_string)
  2209.       prompt_again ();
  2210.  
  2211.     /* We want to remove quoted newlines (that is, a \<newline> pair)
  2212.        unless we are within single quotes or pass_next_character is
  2213.        set (the shell equivalent of literal-next). */
  2214.     character = shell_getc
  2215.       ((current_delimiter () != '\'') && (!pass_next_character));
  2216.       }
  2217.  
  2218.   got_token:
  2219.  
  2220.     token[token_index] = '\0';
  2221.     
  2222.     if ((delimiter_depth || dollar_paren_level || dollar_bracket_level) &&
  2223.     character == EOF)
  2224.       {
  2225.     char reporter = '\0';
  2226.  
  2227.     if (!delimiter_depth)
  2228.       {
  2229.         if (dollar_paren_level)
  2230.           reporter = ')';
  2231.         else if (dollar_bracket_level)
  2232.           reporter = ']';
  2233.       }
  2234.  
  2235.     if (!reporter)
  2236.       reporter = current_delimiter ();
  2237.  
  2238.     report_error ("unexpected EOF while looking for `%c'", reporter);
  2239.     return (-1);
  2240.       }
  2241.  
  2242.     if (all_digits)
  2243.       {
  2244.     /* Check to see what thing we should return.  If the last_read_token
  2245.        is a `<', or a `&', or the character which ended this token is
  2246.        a '>' or '<', then, and ONLY then, is this input token a NUMBER.
  2247.        Otherwise, it is just a word, and should be returned as such. */
  2248.  
  2249.     if (character == '<' || character == '>' ||
  2250.         last_read_token == LESS_AND || last_read_token == GREATER_AND)
  2251.       {
  2252.         yylval.number = atoi (token);
  2253.         return (NUMBER);
  2254.       }
  2255.       }
  2256.  
  2257.     /* Handle special case.  IN is recognized if the last token
  2258.        was WORD and the token before that was FOR or CASE. */
  2259.     if ((last_read_token == WORD) &&
  2260. #if defined (SELECT_COMMAND)
  2261.     ((token_before_that == FOR) || (token_before_that == CASE) || (token_before_that == SELECT)) &&
  2262. #else
  2263.     ((token_before_that == FOR) || (token_before_that == CASE)) &&
  2264. #endif
  2265.     (token[0] == 'i' && token[1] == 'n' && !token[2]))
  2266.       {
  2267.     if (token_before_that == CASE)
  2268.       {
  2269.         in_case_pattern_list = 1;
  2270.         allow_esac_as_next++;
  2271.       }
  2272.     return (IN);
  2273.       }
  2274.  
  2275.     /* Ditto for DO in the FOR case. */
  2276. #if defined (SELECT_COMMAND)
  2277.     if ((last_read_token == WORD) && ((token_before_that == FOR) || (token_before_that == SELECT)) &&
  2278. #else
  2279.     if ((last_read_token == WORD) && (token_before_that == FOR) &&
  2280. #endif
  2281.     (token[0] == 'd' && token[1] == 'o' && !token[2]))
  2282.       return (DO);
  2283.  
  2284.     /* Ditto for ESAC in the CASE case. 
  2285.        Specifically, this handles "case word in esac", which is a legal
  2286.        construct, certainly because someone will pass an empty arg to the
  2287.        case construct, and we don't want it to barf.  Of course, we should
  2288.        insist that the case construct has at least one pattern in it, but
  2289.        the designers disagree. */
  2290.     if (allow_esac_as_next)
  2291.       {
  2292.     allow_esac_as_next--;
  2293.     if (STREQ (token, "esac"))
  2294.       {
  2295.         in_case_pattern_list = 0;
  2296.         return (ESAC);
  2297.       }
  2298.       }
  2299.  
  2300.     /* Ditto for `{' in the FUNCTION case. */
  2301.     if (allow_open_brace)
  2302.       {
  2303.     allow_open_brace = 0;
  2304.     if (token[0] == '{' && !token[1])
  2305.       {
  2306.         open_brace_awaiting_satisfaction++;
  2307.         return ('{');
  2308.       }
  2309.       }
  2310.  
  2311.     if (posixly_correct)
  2312.       CHECK_FOR_RESERVED_WORD (token);
  2313.  
  2314. #if defined (ALIAS)
  2315.     /* OK, we have a token.  Let's try to alias expand it, if (and only if)
  2316.        it's eligible. 
  2317.  
  2318.        It is eligible for expansion if the shell is in interactive mode, and
  2319.        the token is unquoted and the last token read was a command
  2320.        separator (or expand_next_token is set), and we are currently
  2321.        processing an alias (pushed_string_list is non-empty) and this
  2322.        token is not the same as the current or any previously
  2323.        processed alias.
  2324.  
  2325.        Special cases that disqualify:
  2326.      In a pattern list in a case statement (in_case_pattern_list). */
  2327.     if (interactive_shell && !quoted && !in_case_pattern_list &&
  2328.     (expand_next_token || command_token_position (last_read_token)))
  2329.       {
  2330.     char *alias_expand_word (), *expanded;
  2331.  
  2332.     if (expanded_token_stack && token_has_been_expanded (token))
  2333.       goto no_expansion;
  2334.  
  2335.     expanded = alias_expand_word (token);
  2336.     if (expanded)
  2337.       {
  2338.         int len = strlen (expanded), expand_next;
  2339.  
  2340.         /* Erase the current token. */
  2341.         token_index = 0;
  2342.  
  2343.         expand_next = (expanded[len - 1] == ' ') ||
  2344.               (expanded[len - 1] == '\t');
  2345.  
  2346.         push_string (expanded, expand_next, token);
  2347.         goto re_read_token;
  2348.       }
  2349.     else
  2350.       /* This is an eligible token that does not have an expansion. */
  2351. no_expansion:
  2352.       expand_next_token = 0;
  2353.       }
  2354.     else
  2355.       {
  2356.     expand_next_token = 0;
  2357.       }
  2358. #endif /* ALIAS */
  2359.  
  2360.     if (!posixly_correct)
  2361.       CHECK_FOR_RESERVED_WORD (token);
  2362.  
  2363.     /* What if we are attempting to satisfy an open-brace grouper? */
  2364.     if (open_brace_awaiting_satisfaction && token[0] == '}' && !token[1])
  2365.       {
  2366.     open_brace_awaiting_satisfaction--;
  2367.     return ('}');
  2368.       }
  2369.  
  2370.     the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
  2371.     the_word->word = xmalloc (1 + token_index);
  2372.     strcpy (the_word->word, token);
  2373.     the_word->dollar_present = dollar_present;
  2374.     the_word->quoted = quoted;
  2375.     the_word->assignment = assignment (token);
  2376.  
  2377.     yylval.word = the_word;
  2378.     result = WORD;
  2379.  
  2380.     /* A word is an assignment if it appears at the beginning of a
  2381.        simple command, or after another assignment word.  This is
  2382.        context-dependent, so it cannot be handled in the grammar. */
  2383.     if (assignment_acceptable (last_read_token) && the_word->assignment)
  2384.       result = ASSIGNMENT_WORD;
  2385.  
  2386.     if (last_read_token == FUNCTION)
  2387.       allow_open_brace = 1;
  2388.   }
  2389.   return (result);
  2390. }
  2391.  
  2392. /* Return 1 if TOKEN is a token that after being read would allow
  2393.    a reserved word to be seen, else 0. */
  2394. static int
  2395. reserved_word_acceptable (token)
  2396.      int token;
  2397. {
  2398. #if 0
  2399.   if (member (token, "\n;()|&{") ||
  2400. #else
  2401.   if (token == '\n' || token == ';' || token == '(' || token == ')' ||
  2402.       token == '|' || token == '&' || token == '{' ||
  2403. #endif
  2404.       token == '}' ||            /* XXX */
  2405.       token == AND_AND ||
  2406.       token == BANG ||
  2407.       token == DO ||
  2408.       token == ELIF ||
  2409.       token == ELSE ||
  2410.       token == FI ||
  2411.       token == IF ||
  2412.       token == OR_OR ||
  2413.       token == SEMI_SEMI ||
  2414.       token == THEN ||
  2415.       token == UNTIL ||
  2416.       token == WHILE ||
  2417.       token == DONE ||        /* XXX these two are experimental */
  2418.       token == ESAC ||
  2419.       token == 0)
  2420.     return (1);
  2421.   else
  2422.     return (0);
  2423. }
  2424.  
  2425. /* Return the index of TOKEN in the alist of reserved words, or -1 if
  2426.    TOKEN is not a shell reserved word. */
  2427. int
  2428. find_reserved_word (token)
  2429.      char *token;
  2430. {
  2431.   int i;
  2432.   for (i = 0; word_token_alist[i].word != (char *)NULL; i++)
  2433.     if (STREQ (token, word_token_alist[i].word))
  2434.       return i;
  2435.   return -1;
  2436. }
  2437.  
  2438. #if defined (READLINE)
  2439. /* Called after each time readline is called.  This insures that whatever
  2440.    the new prompt string is gets propagated to readline's local prompt
  2441.    variable. */
  2442. static void
  2443. reset_readline_prompt ()
  2444. {
  2445.   if (prompt_string_pointer && *prompt_string_pointer)
  2446.     {
  2447.       char *temp_prompt;
  2448.  
  2449.       temp_prompt = decode_prompt_string (*prompt_string_pointer);
  2450.  
  2451.       if (!temp_prompt)
  2452.     temp_prompt = savestring ("");
  2453.  
  2454.       FREE (current_readline_prompt);
  2455.  
  2456.       current_readline_prompt = temp_prompt;
  2457.     }
  2458. }
  2459. #endif /* READLINE */
  2460.  
  2461. #if defined (HISTORY)
  2462. /* A list of tokens which can be followed by newlines, but not by
  2463.    semi-colons.  When concatenating multiple lines of history, the
  2464.    newline separator for such tokens is replaced with a space. */
  2465. static int no_semi_successors[] = {
  2466.   '\n', '{', '(', ')', ';', '&', '|',
  2467.   CASE, DO, ELSE, IF, IN, SEMI_SEMI, THEN, UNTIL, WHILE, AND_AND, OR_OR,
  2468.   0
  2469. };
  2470.  
  2471. /* If we are not within a delimited expression, try to be smart
  2472.    about which separators can be semi-colons and which must be
  2473.    newlines. */
  2474. char *
  2475. history_delimiting_chars ()
  2476. {
  2477.   if (!delimiter_depth)
  2478.     {
  2479.       register int i;
  2480.  
  2481.       for (i = 0; no_semi_successors[i]; i++)
  2482.     {
  2483.       if (token_before_that == no_semi_successors[i])
  2484.         return (" ");
  2485.     }
  2486.       return ("; ");
  2487.     }
  2488.   else
  2489.     return ("\n");
  2490. }
  2491. #endif /* HISTORY */
  2492.  
  2493. /* Issue a prompt, or prepare to issue a prompt when the next character
  2494.    is read. */
  2495. static void
  2496. prompt_again ()
  2497. {
  2498.   char *temp_prompt;
  2499.  
  2500.   if (!interactive)    /* XXX */
  2501.     return;
  2502.  
  2503.   ps1_prompt = get_string_value ("PS1");
  2504.   ps2_prompt = get_string_value ("PS2");
  2505.  
  2506.   if (!prompt_string_pointer)
  2507.     prompt_string_pointer = &ps1_prompt;
  2508.  
  2509.   if (*prompt_string_pointer)
  2510.     temp_prompt = decode_prompt_string (*prompt_string_pointer);
  2511.   else
  2512.     temp_prompt = savestring ("");
  2513.  
  2514.   current_prompt_string = *prompt_string_pointer;
  2515.   prompt_string_pointer = &ps2_prompt;
  2516.  
  2517. #if defined (READLINE)
  2518.   if (!no_line_editing)
  2519.     {
  2520.       FREE (current_readline_prompt);
  2521.       current_readline_prompt = temp_prompt;
  2522.     }
  2523.   else
  2524. #endif    /* READLINE */
  2525.     {
  2526.       FREE (current_decoded_prompt);
  2527.       current_decoded_prompt = temp_prompt;
  2528.     }
  2529. }
  2530.  
  2531. static void
  2532. print_prompt ()
  2533. {
  2534.   fprintf (stderr, "%s", current_decoded_prompt);
  2535.   fflush (stderr);
  2536. }
  2537.  
  2538. /* Return a string which will be printed as a prompt.  The string
  2539.    may contain special characters which are decoded as follows:
  2540.    
  2541.     \t    the time
  2542.     \d    the date
  2543.     \n    CRLF
  2544.     \s    the name of the shell
  2545.     \w    the current working directory
  2546.     \W    the last element of PWD
  2547.     \u    your username
  2548.     \h    the hostname
  2549.     \#    the command number of this command
  2550.     \!    the history number of this command
  2551.     \$    a $ or a # if you are root
  2552.     \<octal> character code in octal
  2553.     \\    a backslash
  2554. */
  2555. #define PROMPT_GROWTH 50
  2556. char *
  2557. decode_prompt_string (string)
  2558.      char *string;
  2559. {
  2560.   int result_size = PROMPT_GROWTH;
  2561.   int result_index = 0;
  2562.   char *result;
  2563.   int c;
  2564.   char *temp = (char *)NULL;
  2565.   WORD_LIST *list;
  2566.  
  2567. #if defined (PROMPT_STRING_DECODE)
  2568.  
  2569.   result = xmalloc (PROMPT_GROWTH);
  2570.   result[0] = 0;
  2571.  
  2572.   while (c = *string++)
  2573.     {
  2574.       if (posixly_correct && c == '!')
  2575.     {
  2576.       if (*string == '!')
  2577.         {
  2578.           temp = savestring ("!");
  2579.           goto add_string;
  2580.         }
  2581.       else
  2582.         {
  2583. #if !defined (HISTORY)
  2584.         temp = savestring ("1");
  2585. #else /* HISTORY */
  2586.         temp = itos (history_number ());
  2587. #endif /* HISTORY */
  2588.         string--;    /* add_string increments string again. */
  2589.         goto add_string;
  2590.         }
  2591.     } 
  2592.       if (c == '\\')
  2593.     {
  2594.       c = *string;
  2595.  
  2596.       switch (c)
  2597.         {
  2598.         case '0':
  2599.         case '1':
  2600.         case '2':
  2601.         case '3':
  2602.         case '4':
  2603.         case '5':
  2604.         case '6':
  2605.         case '7':
  2606.           {
  2607.         char octal_string[4];
  2608.         int n;
  2609.  
  2610.         strncpy (octal_string, string, 3);
  2611.         octal_string[3] = '\0';
  2612.  
  2613.         n = read_octal (octal_string);
  2614.  
  2615.         temp = savestring ("\\");
  2616.         if (n != -1)
  2617.           {
  2618.             string += 3;
  2619.             temp[0] = n;
  2620.           }
  2621.  
  2622.         c = 0;
  2623.         goto add_string;
  2624.           }
  2625.       
  2626.         case 't':
  2627.         case 'd':
  2628.           /* Make the current time/date into a string. */
  2629.           {
  2630.         time_t the_time = time (0);
  2631.         char *ttemp = ctime (&the_time);
  2632.         temp = savestring (ttemp);
  2633.  
  2634.         if (c == 't')
  2635.           {
  2636.             strcpy (temp, temp + 11);
  2637.             temp[8] = '\0';
  2638.           }
  2639.         else
  2640.           temp[10] = '\0';
  2641.  
  2642.         goto add_string;
  2643.           }
  2644.  
  2645.         case 'n':
  2646.           if (!no_line_editing)
  2647.         temp = savestring ("\r\n");
  2648.           else
  2649.         temp = savestring ("\n");
  2650.           goto add_string;
  2651.  
  2652.         case 's':
  2653.           {
  2654.         temp = base_pathname (shell_name);
  2655.         temp = savestring (temp);
  2656.         goto add_string;
  2657.           }
  2658.     
  2659.         case 'w':
  2660.         case 'W':
  2661.           {
  2662.         /* Use the value of PWD because it is much more effecient. */
  2663. #define EFFICIENT
  2664. #ifdef EFFICIENT
  2665.         char *polite_directory_format (), t_string[MAXPATHLEN];
  2666.  
  2667.         temp = get_string_value ("PWD");
  2668.  
  2669.         if (!temp)
  2670.           getwd (t_string);
  2671.         else
  2672.           strcpy (t_string, temp);
  2673. #else
  2674.         getwd (t_string);
  2675. #endif    /* EFFICIENT */
  2676.  
  2677.         if (c == 'W')
  2678.           {
  2679.             char *dir = (char *)strrchr (t_string, '/');
  2680.             if (dir && dir != t_string)
  2681.               strcpy (t_string, dir + 1);
  2682.             temp = savestring (t_string);
  2683.           }
  2684.         else
  2685.           temp = savestring (polite_directory_format (t_string));
  2686.         goto add_string;
  2687.           }
  2688.       
  2689.         case 'u':
  2690.           {
  2691.         temp = savestring (current_user.user_name);
  2692.         goto add_string;
  2693.           }
  2694.  
  2695.         case 'h':
  2696.           {
  2697.         char *t_string;
  2698.  
  2699.         temp = savestring (current_host_name);
  2700.         if (t_string = (char *)strchr (temp, '.'))
  2701.           *t_string = '\0';
  2702.         goto add_string;
  2703.           }
  2704.  
  2705.         case '#':
  2706.           {
  2707.         temp = itos (current_command_number);
  2708.         goto add_string;
  2709.           }
  2710.  
  2711.         case '!':
  2712.           {
  2713. #if !defined (HISTORY)
  2714.         temp = savestring ("1");
  2715. #else /* HISTORY */
  2716.         temp = itos (history_number ());
  2717. #endif /* HISTORY */
  2718.         goto add_string;
  2719.           }
  2720.  
  2721.         case '$':
  2722.           temp = savestring (geteuid () == 0 ? "#" : "$");
  2723.           goto add_string;
  2724.  
  2725.         case '[':
  2726.         case ']':
  2727.           temp = xmalloc(3);
  2728.           temp[0] = '\001';
  2729.           temp[1] = (c == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
  2730.           temp[2] = '\0';
  2731.           goto add_string;
  2732.  
  2733.         case '\\':
  2734.           temp = savestring ("\\");
  2735.           goto add_string;
  2736.  
  2737.         default:
  2738.           temp = savestring ("\\ ");
  2739.           temp[1] = c;
  2740.  
  2741.         add_string:
  2742.           if (c)
  2743.         string++;
  2744.           result =
  2745.         sub_append_string (temp, result, &result_index, &result_size);
  2746.           temp = (char *)NULL; /* Free ()'ed in sub_append_string (). */
  2747.           result[result_index] = '\0';
  2748.           break;
  2749.         }
  2750.     }
  2751.       else
  2752.     {
  2753.       while (3 + result_index > result_size)
  2754.         result = xrealloc (result, result_size += PROMPT_GROWTH);
  2755.  
  2756.       result[result_index++] = c;
  2757.       result[result_index] = '\0';
  2758.     }
  2759.     }
  2760. #else /* !PROMPT_STRING_DECODE */
  2761.   result = savestring (string);
  2762. #endif /* !PROMPT_STRING_DECODE */
  2763.  
  2764.   /* Perform variable and parameter expansion and command substitution on
  2765.      the prompt string. */
  2766.   list = expand_string (result, 1);
  2767.   free (result);
  2768.   result = string_list (list);
  2769.   dispose_words (list);
  2770.  
  2771.   return (result);
  2772. }
  2773.  
  2774. /* Report a syntax error, and restart the parser.  Call here for fatal
  2775.    errors. */
  2776. yyerror ()
  2777. {
  2778.   report_syntax_error ((char *)NULL);
  2779.   reset_parser ();
  2780. }
  2781.  
  2782. /* Report a syntax error with line numbers, etc.
  2783.    Call here for recoverable errors.  If you have a message to print,
  2784.    then place it in MESSAGE, otherwise pass NULL and this will figure
  2785.    out an appropriate message for you. */
  2786. static void
  2787. report_syntax_error (message)
  2788.      char *message;
  2789. {
  2790.   if (message)
  2791.     {
  2792.       if (!interactive)
  2793.     {
  2794.       char *name = bash_input.name ? bash_input.name : "stdin";
  2795.       report_error ("%s: line %d: `%s'", name, line_number, message);
  2796.     }
  2797.       else
  2798.     {
  2799.       if (EOF_Reached)
  2800.         EOF_Reached = 0;
  2801.       report_error ("%s", message);
  2802.     }
  2803.  
  2804.       last_command_exit_value = EX_USAGE;
  2805.       return;
  2806.     }
  2807.  
  2808.   if (shell_input_line && *shell_input_line)
  2809.     {
  2810.       char *t = shell_input_line;
  2811.       register int i = shell_input_line_index;
  2812.       int token_end = 0;
  2813.  
  2814.       if (!t[i] && i)
  2815.     i--;
  2816.  
  2817.       while (i && (t[i] == ' ' || t[i] == '\t' || t[i] == '\n'))
  2818.     i--;
  2819.  
  2820.       if (i)
  2821.     token_end = i + 1;
  2822.  
  2823.       while (i && !member (t[i], " \n\t;|&"))
  2824.     i--;
  2825.  
  2826.       while (i != token_end && member (t[i], " \t\n"))
  2827.     i++;
  2828.  
  2829.       if (token_end)
  2830.     {
  2831.       char *error_token;
  2832.       error_token = xmalloc (1 + (token_end - i));
  2833.       strncpy (error_token, t + i, token_end - i);
  2834.       error_token[token_end - i] = '\0';
  2835.  
  2836.       report_error ("syntax error near unexpected token `%s'", error_token);
  2837.       free (error_token);
  2838.     }
  2839.       else if ((i == 0) && (token_end == 0))    /* a 1-character token */
  2840.     {
  2841.       char etoken[2];
  2842.       etoken[0] = t[i];
  2843.       etoken[1] = '\0';
  2844.  
  2845.       report_error ("syntax error near unexpected token `%s'", etoken);
  2846.     }
  2847.  
  2848.       if (!interactive)
  2849.     {
  2850.       char *temp = savestring (shell_input_line);
  2851.       char *name = bash_input.name ? bash_input.name : "stdin";
  2852.       int l = strlen (temp);
  2853.  
  2854.       while (l && temp[l - 1] == '\n')
  2855.         temp[--l] = '\0';
  2856.  
  2857.       report_error ("%s: line %d: `%s'", name, line_number, temp);
  2858.       free (temp);
  2859.     }
  2860.     }
  2861.   else
  2862.     {
  2863.       char *name, *msg;
  2864.       if (!interactive)
  2865.     name = bash_input.name ? bash_input.name : "stdin";
  2866.       if (EOF_Reached)
  2867.     msg = "syntax error: unexpected end of file";
  2868.       else
  2869.     msg = "syntax error";
  2870.       if (!interactive)
  2871.     report_error ("%s: line %d: %s", name, line_number, msg);
  2872.       else
  2873.     {
  2874.       /* This file uses EOF_Reached only for error reporting
  2875.          when the shell is interactive.  Other mechanisms are 
  2876.          used to decide whether or not to exit. */
  2877.       EOF_Reached = 0;
  2878.       report_error (msg);
  2879.     }
  2880.     }
  2881.   last_command_exit_value = EX_USAGE;
  2882. }
  2883.  
  2884. /* ??? Needed function. ??? We have to be able to discard the constructs
  2885.    created during parsing.  In the case of error, we want to return
  2886.    allocated objects to the memory pool.  In the case of no error, we want
  2887.    to throw away the information about where the allocated objects live.
  2888.    (dispose_command () will actually free the command. */
  2889. discard_parser_constructs (error_p)
  2890.      int error_p;
  2891. {
  2892. }
  2893.    
  2894. /* Do that silly `type "bye" to exit' stuff.  You know, "ignoreeof". */
  2895.  
  2896. /* A flag denoting whether or not ignoreeof is set. */
  2897. int ignoreeof = 0;
  2898.  
  2899. /* The number of times that we have encountered an EOF character without
  2900.    another character intervening.  When this gets above the limit, the
  2901.    shell terminates. */
  2902. int eof_encountered = 0;
  2903.  
  2904. /* The limit for eof_encountered. */
  2905. int eof_encountered_limit = 10;
  2906.  
  2907. /* If we have EOF as the only input unit, this user wants to leave
  2908.    the shell.  If the shell is not interactive, then just leave.
  2909.    Otherwise, if ignoreeof is set, and we haven't done this the
  2910.    required number of times in a row, print a message. */
  2911. static void
  2912. handle_eof_input_unit ()
  2913. {
  2914.   if (interactive)
  2915.     {
  2916.       /* shell.c may use this to decide whether or not to write out the
  2917.      history, among other things.  We use it only for error reporting
  2918.      in this file. */
  2919.       if (EOF_Reached)
  2920.     EOF_Reached = 0;
  2921.  
  2922.       /* If the user wants to "ignore" eof, then let her do so, kind of. */
  2923.       if (ignoreeof)
  2924.     {
  2925.       if (eof_encountered < eof_encountered_limit)
  2926.         {
  2927.           fprintf (stderr, "Use \"%s\" to leave the shell.\n",
  2928.                login_shell ? "logout" : "exit");
  2929.           eof_encountered++;
  2930.           /* Reset the prompt string to be $PS1. */
  2931.           prompt_string_pointer = (char **)NULL;
  2932.           prompt_again ();
  2933.           last_read_token = current_token = '\n';
  2934.           return;
  2935.         } 
  2936.     }
  2937.  
  2938.       /* In this case EOF should exit the shell.  Do it now. */
  2939.       reset_parser ();
  2940.       exit_builtin ((WORD_LIST *)NULL);
  2941.     }
  2942.   else
  2943.     {
  2944.       /* We don't write history files, etc., for non-interactive shells. */
  2945.       EOF_Reached = 1;
  2946.     }
  2947. }
  2948.